大家好,欢迎来到IT知识分享网。
摘要:韦东山android视频学习笔记
面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一
图一
1、我们在第2课第2节_Java面向对象编程_封装性_P 中定义了persion.我在这代码基础上定义一个学习类,并继承persion类。
1 class Persion{ 2 private int age; 3 4 public void setAge(int age){ 5 if (age < 0 || age > 200) 6 age = 0; 7 else 8 this.age = age; 9 } 10 11 public int getAge(){ 12 return age; 13 } 14 } 15 16 class Student extends Persion{ 17 18 } 19 20 public class Ext{ 21 public static void main(String args[]){ 22 Student stu = new Student(); 23 stu.setAge(10); 24 25 System.out.println(stu.getAge()); 26 27 } 28 }
编译运行结果
2、在上面的代码基础上,我们的子类继承了父类,我们可以添加自己的属性还有方法.
1 class Persion{ 2 private int age; 3 4 public void setAge(int age){ 5 if (age < 0 || age > 200) 6 age = 0; 7 else 8 this.age = age; 9 } 10 11 public int getAge(){ 12 return age; 13 } 14 } 15 16 class Student extends Persion{ 17 private String school; 18 19 public void setSchool(String school){ 20 this.school = school; 21 } 22 23 public String getSchool(){ 24 return school; 25 } 26 27 public Student(String school){ 28 this.school = school; 29 } 30 31 32 } 33 34 public class Ext2{ 35 public static void main(String args[]){ 36 Student stu = new Student("ustc"); 37 stu.setAge(10); 38 39 System.out.println(stu.getAge()); 40 System.out.println(stu.getSchool()); 41 42 } 43 }
编译运行结果
3、子类继承父类的方法和属性都可以进行覆写,我们在子类覆写父类的printInfo方法。
1 class Persion{ 2 private int age; 3 4 public void setAge(int age){ 5 if (age < 0 || age > 200) 6 age = 0; 7 else 8 this.age = age; 9 } 10 11 public int getAge(){ 12 return age; 13 } 14 15 public void printInfo(){ 16 System.out.println("age = " + age); 17 } 18 } 19 20 class Student extends Persion{ 21 private String school; 22 23 public void setSchool(String school){ 24 this.school = school; 25 } 26 27 public String getSchool(){ 28 return school; 29 } 30 31 public Student(String school){ 32 this.school = school; 33 } 34 35 public void printInfo(){ 36 System.out.println("school = " + school + " age = " + getAge()); 37 } 38 } 39 40 public class Ext3{ 41 public static void main(String args[]){ 42 Student stu = new Student("ustc"); 43 stu.setAge(10); 44 45 System.out.println(stu.getAge()); 46 System.out.println(stu.getSchool()); 47 stu.printInfo(); 48 49 } 50 }
编译运行结果
4、实例化子类对象时,先调用父类的构造方法,再调用子类的构造方法,super()函数在子类中可以指定调用父类构造函数的类型。
1 class Persion{ 2 private int age; 3 4 public void setAge(int age){ 5 if (age < 0 || age > 200) 6 age = 0; 7 else 8 this.age = age; 9 } 10 11 public int getAge(){ 12 return age; 13 } 14 15 public void printInfo(){ 16 System.out.println("age = " + age); 17 } 18 19 public Persion(){System.out.println("Persion()");}; 20 public Persion(int age){ 21 System.out.println("Persion(int age)"); 22 this.age = age; 23 } 24 } 25 26 class Student extends Persion{ 27 private String school; 28 29 public void setSchool(String school){ 30 this.school = school; 31 } 32 33 public String getSchool(){ 34 return school; 35 } 36 37 public Student(String school){ 38 super(5); 39 System.out.println("Student(String school)"); 40 this.school = school; 41 } 42 43 public void printInfo(){ 44 System.out.println("school = " + school + " age = " + getAge()); 45 } 46 } 47 48 public class Ext4{ 49 public static void main(String args[]){ 50 Student stu = new Student("ustc"); 51 //stu.setAge(10); 52 53 System.out.println(stu.getAge()); 54 System.out.println(stu.getSchool()); 55 stu.printInfo(); 56 57 } 58 }
编译运行结果
5、抽象类规定子类必须实现的方法,起“模板”作用,缺点不能实例化对象,子类必须覆写全部抽象方法
1 abstract class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public abstract void study(); 8 } 9 10 class Son extends Father{ 11 public void study() {System.out.println("I am study"); } 12 13 } 14 15 public class Ext6 { 16 public static void main (String args[]) { 17 //Father f = new Father(); 18 Son son = new Son(); 19 son.study(); 20 21 } 22 }
编译运行结果:
6、作用:跟抽象类相似,起“模板”作用;子类可以继承多个接口,突破“单继承”的限制
1 abstract class Father{ 2 private int money; 3 4 public int getMoney(){return money;} 5 public void setMoney(int money){ 6 this.money = money; 7 } 8 public abstract void study(); 9 } 10 11 12 interface A{ 13 public static final int i = 10; 14 public abstract int getNum(); 15 } 16 17 interface B{ 18 public static String name = "InterfaceB"; 19 public abstract String getName(); 20 } 21 22 23 24 class Son extends Father implements A,B{ 25 public int getNum(){return i;} 26 public String getName(){return name;} 27 28 public void study(){ 29 System.out.println("I am study"); 30 } 31 } 32 33 34 public class Ext6{ 35 public static void main(String args[]){ 36 Son son = new Son(); 37 System.out.println(son.getName()); 38 System.out.println(son.getNum()); 39 son.study(); 40 41 } 42 }
编译运行结果:
相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask
觉得不错,就给我点小支持吧,蟹蟹
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/27759.html