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


    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

  • 相关阅读:
    npx vs npm
    RubyGem镜像/ruby国内镜像
    IOS开发依赖管理工具CocoaPods
    alpine linux
    阿里妈妈图标库
    java应用系统运行速度慢的解决方法
    jvm程序执行慢诊断手册
    js强制不使用“兼容性视图”
    java.lang.NumberFormatException: Infinite or NaN
    ALTER添加列后,立即UPDATE该列会报错
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7252941.html
Copyright © 2020-2023  润新知