当前位置:   article > 正文

Android java基础_类的继承

Android java基础_类的继承

一.Android Java基础_类的继承

先封装一个persion类,在persion的基础上定义Student类,并基础persion类。

子类能访问父类的成员函数。

  1. class Person {
  2. private int age;
  3. public void setAge(int age) {
  4. if (age < 0 || age > 200)
  5. age = 0;
  6. else {
  7. this.age = age;
  8. }
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. }
  14. class Student extends Person{
  15. }
  16. public class Ext {
  17. public static void main (String args[]) {
  18. Student stu = new Student();
  19. stu.setAge(10);
  20. System.out.println(stu.getAge());
  21. }
  22. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext
  3. 10

在上面的代码基础上,我们的子类继承了父类,我们可以添加自己的属性还有方法.

  1. class Person {
  2. private int age;
  3. public void setAge(int age) {
  4. if (age < 0 || age > 200)
  5. age = 0;
  6. else {
  7. this.age = age;
  8. }
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. }
  14. class Student extends Person{
  15. private String school;
  16. public void setSchool(String school) {
  17. this.school = school;
  18. }
  19. public String getSchool() {
  20. return school;
  21. }
  22. public Student(String school) {
  23. this.school = school;
  24. }
  25. }
  26. public class Ext2 {
  27. public static void main (String args[]) {
  28. Student stu = new Student("ustc");
  29. stu.setAge(10);
  30. System.out.println(stu.getAge());
  31. System.out.println(stu.getSchool());
  32. }
  33. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext2.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext2
  3. 10
  4. ustc

子类继承父类的方法和属性都可以进行覆写,我们在子类覆写父类的printInfo方法。

  1. class Person {
  2. private int age;
  3. public void setAge(int age) {
  4. if (age < 0 || age > 200)
  5. age = 0;
  6. else {
  7. this.age = age;
  8. }
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void printInfo() {
  14. System.out.println("age = "+age);
  15. }
  16. }
  17. class Student extends Person{
  18. private String school;
  19. public void setSchool(String school) {
  20. this.school = school;
  21. }
  22. public String getSchool() {
  23. return school;
  24. }
  25. public Student(String school) {
  26. this.school = school;
  27. }
  28. /* override */
  29. public void printInfo() {
  30. System.out.println("school = "+school+"; age = "+getAge());
  31. }
  32. }
  33. public class Ext3 {
  34. public static void main (String args[]) {
  35. Student stu = new Student("ustc");
  36. stu.setAge(10);
  37. System.out.println(stu.getAge());
  38. System.out.println(stu.getSchool());
  39. stu.printInfo();
  40. }
  41. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext3.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext3
  3. 10
  4. ustc
  5. school = ustc; age = 10

实例化子类对象时,先调用父类的构造方法,再调用子类的构造方法,super()函数在子类中可以指定调用父类构造函数的类型。

  1. class Person {
  2. private int age;
  3. public void setAge(int age) {
  4. if (age < 0 || age > 200)
  5. age = 0;
  6. else {
  7. this.age = age;
  8. }
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void printInfo() {
  14. System.out.println("age = "+age);
  15. }
  16. public Person () {System.out.println("Person ()");}
  17. public Person (int age) {
  18. System.out.println("Person (int age)");
  19. this.age = age;
  20. }
  21. }
  22. class Student extends Person{
  23. private String school;
  24. public void setSchool(String school) {
  25. this.school = school;
  26. }
  27. public String getSchool() {
  28. return school;
  29. }
  30. public Student(String school) {
  31. /* will call the super() */
  32. //super();
  33. super(5);
  34. System.out.println("Student(String school)");
  35. this.school = school;
  36. }
  37. /* override */
  38. public void printInfo() {
  39. System.out.println("school = "+school+"; age = "+getAge());
  40. }
  41. }
  42. public class Ext4 {
  43. public static void main (String args[]) {
  44. Student stu = new Student("ustc");
  45. //stu.setAge(10);
  46. System.out.println(stu.getAge());
  47. System.out.println(stu.getSchool());
  48. stu.printInfo();
  49. }
  50. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext4.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext4
  3. Person (int age)
  4. Student(String school)
  5. 5
  6. ustc
  7. school = ustc; age = 5

抽象类规定子类必须实现的方法,起“模板”作用,缺点不能实例化对象,子类必须覆写全部抽象方法。

  1. abstract class Father {
  2. private int money;
  3. public int getMoney() {return money; }
  4. public void setMoney(int money) {this.money = money; }
  5. public abstract void study();
  6. }
  7. class Son extends Father{
  8. public void study() {System.out.println("I am study"); }
  9. }
  10. public class Ext6 {
  11. public static void main (String args[]) {
  12. //Father f = new Father();
  13. Son son = new Son();
  14. son.study();
  15. }
  16. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext6.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext6
  3. I am study

作用:跟抽象类相似,起“模板”作用;子类可以继承多个接口,突破“单继承”的限制。

  1. abstract class Father {
  2. private int money;
  3. public int getMoney() {return money; }
  4. public void setMoney(int money) {this.money = money; }
  5. public abstract void study();
  6. }
  7. class Son extends Father{
  8. public void study() {System.out.println("I am study"); }
  9. }
  10. public class Ext6 {
  11. public static void main (String args[]) {
  12. //Father f = new Father();
  13. Son son = new Son();
  14. son.study();
  15. }
  16. }

运行结果:

  1. root@ubuntu:/home/topeet/guyilian# javac Ext7.java
  2. root@ubuntu:/home/topeet/guyilian# java Ext7
  3. InterfaceB
  4. I am study

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/110080
推荐阅读
相关标签
  

闽ICP备14008679号