抽象类接口多态总结之四

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象类接口多态总结之四相关的知识,希望对你有一定的参考价值。

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



以上是关于抽象类接口多态总结之四的主要内容,如果未能解决你的问题,请参考以下文章

php面向对象 封装继承多态 接口重载抽象类最终类总结

C++之多态总结(多态的定义及实现,抽象类,多态原理,单继承,多继承中的虚函数表)

面向对象之多态性(接口类)和抽象类

C#程序设计之抽象类多态和接口

继承 多态 抽象类 接口等学习内容总结

继承,多态,接口与抽象类学习总结