• 抽象类、接口、多态总结之四


    1、为抽象类实例化

     1 abstract class AA{
     2     public abstract void print();
     3 }
     4 class BB extends AA{
     5     public void print() {
     6         System.out.println("hello");
     7     }
     8 }
     9 
    10 public class AbstractCaseDemo01 {
    11 
    12     public static void main(String[] args) {
    13         AA a = new BB();
    14         a.print();
    15     }
    16 
    17 }

    输出结果为:hello

    2、为接口实例化

     1 interface AA{
     2     public abstract void print();
     3 }
     4 class BB implements AA{
     5     public void print() {
     6         System.out.println("hello");
     7     }
     8 }
     9 
    10 public class InterfaceCaseDemo01 {
    11     public static void main(String[] args) {
    12         AA a = new BB();
    13         a.print();
    14     }
    15 }

    输出结果为:hello

    3、抽象类的应用

     1 abstract class Person{
     2     private String name;
     3     private int age;
     4     public Person(String name,int age){
     5         this.name = name;
     6         this.age = age;
     7     }
     8     public String getName() {
     9         return name;
    10     }
    11     public int getAge() {
    12         return age;
    13     }
    14     public void say(){
    15         System.out.println(this.getContent());
    16     }
    17     public abstract String getContent();
    18 }
    19 class Student extends Person{
    20     private float score;
    21     public Student(String name,int age,float score){
    22         super(name, age);
    23         this.score = score;
    24     }
    25     public String getContent() {
    26         return super.getName() + ",,," + super.getAge() + ",," + this.score;
    27     }
    28 }
    29 class Worker extends Person{
    30     private float salary;
    31     public Worker(String name,int age,float salary){
    32         super(name,age);
    33         this.salary = salary;
    34     }
    35     public String getContent() {
    36         return super.getName() + ",,," + super.getAge() + ",,," + this.salary;
    37     }
    38     
    39 }
    40 
    41 public class AbstractCaseDemo02 {
    42 
    43     public static void main(String[] args) {
    44         Person per1 = null;
    45         Person per2 = null;
    46         per1 = new Student("zhangsan", 20, 99.0f);
    47         per2 = new Student("sili", 30, 3000.f);
    48         per1.say();
    49         per2.say();
    50     }
    51 }

    输出结果为:

    zhangsan,,,20,,99.0
    sili,,,30,,3000.0

    4、能够接收子类对象的函数

     1 abstract class Person{
     2     private String name;
     3     private int age;
     4     public Person(String name,int age){
     5         this.name = name;
     6         this.age = age;
     7     }
     8     public String getName() {
     9         return name;
    10     }
    11     public int getAge() {
    12         return age;
    13     }
    14     public void say(){
    15         System.out.println(this.getContent());
    16     }
    17     public abstract String getContent();
    18 }
    19 class Student extends Person{
    20     private float score;
    21     public Student(String name,int age,float score){
    22         super(name, age);
    23         this.score = score;
    24     }
    25     public String getContent() {
    26         return super.getName() + ",,," + super.getAge() + ",," + this.score;
    27     }
    28 }
    29 class Worker extends Person{
    30     private float salary;
    31     public Worker(String name,int age,float salary){
    32         super(name,age);
    33         this.salary = salary;
    34     }
    35     public String getContent() {
    36         return super.getName() + ",,," + super.getAge() + ",,," + this.salary;
    37     }
    38 }
    39 
    40 public class AbstractCaseDemo02 {
    41     public static void main(String[] args) {
    42         Person per1 = null;
    43         Person per2 = null;
    44         per1 = new Student("zhangsan", 20, 99.0f);
    45         per2 = new Student("sili", 30, 3000.f);
    46         fun(per1);
    47         fun(per2);
    48     }
    49     private static void fun(Person p) {
    50         p.say();
    51     }
    52 }

    输出结果:

    zhangsan,,,20,,99.0
    sili,,,30,,3000.0

  • 相关阅读:
    CTS、CLS、CLR分别作何解释?
    C#中 property 与 attribute的区别,他们各有什么用处,这种机制的好处在哪里?
    net中读写数据库需要用到那些类?他们的作用?
    什么叫应用程序域?
    一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
    override与重载的区别
    请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
    求以下表达式的值,写出您想到的一种或几种实现方法: 12+34+……+m
    什么是强类型系统?
    在下面的例子里
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7252941.html
Copyright © 2020-2023  润新知