zoukankan      html  css  js  c++  java
  • 尚学堂--面向对象2

    1、继承中的构造方法

    • 子类的构造过程必须调用父类的构造方法
    • 子类可以在自己的构造方法中使用 super(argument_list)调用父类的构造方法
      •   使用 this(argment_list) 调用本类的另外的构造方法
      •   如果调用super,必须写在子类构造方法的第一行
    • 如果子类的构造方法中没有显示的调用父类构造方法,则系统默认调用父类无参数的构造方法
    • 如果子类构造方法中既没有显式调用父类构造方法,而父类中又没有无参的构造方法,则编译出错
     1 class SuperClass {
     2     private int n;
     3 
     4     SuperClass() {
     5         System.out.println("SuperClass");
     6     }
     7 
     8     SuperClass(int n) {
     9         System.out.println("SuperClass(" + n + ")");//先输出传入的参数
    10         this.n = n;//再把参数赋值给成员变量
    11     }
    12 }
    13 
    14 class SubClass extends SuperClass {
    15     private int n;
    16 
    17     SubClass() {
    18         super(300);
    19         System.out.println("SubClass");
    20     }
    21 
    22     SubClass(int n) {
    23         System.out.println("SubClass(" + n + ")");
    24         this.n = n;
    25     }
    26 }
    27 
    28 public class Test {
    29     public static void main(String[] args) {
    30         SubClass sc1 = new SubClass();
    31         SubClass sc2 = new SubClass(400);
    32     }
    33 }

     这个不太好分析:

     1 class Person {
     2     private String name;
     3     private String location;
     4 
     5     Person(String name) {
     6         this.name = name;
     7         location = "beijing";
     8     }
     9 
    10     Person(String name, String location) {
    11         this.name = name;
    12         this.location = location;
    13     }
    14 
    15     public String info(){
    16         return "name  = " + name + "location = " + location;        
    17     }
    18 }
    19 
    20 class Student extends Person {
    21     private String school;
    22 
    23     Student(String name, String school) {
    24         this(name, "beijing",school);//调用的是Student(String n, String l, String school){}方法
    25     }
    26 
    27     Student(String n, String l, String school) {
    28         super(n,l);
    29         this.school = school;
    30     }
    31     public String info() {
    32         return super.info() + "school" + school;
    33     }
    34 }
    35 
    36 public class Test {
    37     public static void main(String[] args) {
    38         Person p1 = new Person("A");
    39         Person p2 = new Person("B","shanghai");
    40         Student s1 = new Student("C", "S1");
    41         Student s2 = new Student("C", "shanghai", "S2");
    42 
    43         System.out.println(p1.info());
    44         System.out.println(p2.info());
    45         System.out.println(s1.info());
    46         System.out.println(s2.info());
    47     }
    48 }

     2、Object类

    • Object类是所有Java类的根基类
    • 如果在类的声明中未使用extends关键字指明其父类则默认为Object类
    • 在java.lang这个包下(特例:Java中只有使用java.lang中的方法可以不导入java.lang这个包,其他的必须导入包)

    toString方法:

      Object类中定义有public String toString()方法,其返回值是String类型,描述当前对象的相关信息

      再进行String与其他类型数据的连接操作时(如:System.out.println("info" + Person)),将自动调用该对象类的toString()方法

      可以根据需要在用户自定义类型中重写toString()方法

    equals方法:

      

    3、对象转型(对象转型调用的是属性,多态调用的是方法

    一个父类的引用类型变量可以“指向”子类的对象

    一个父类的引用不可以访问其子类对象新增加的成员(属性和方法)

    可以使用  引用 变量 instanceof 类名 来判断该引用型变量所“指向”的对象是否属于该类或该类的子类

    子类的对象可以当作父类的对象来使用称作向上转型(upcasting),反之成为向下转型(downcasting)

    对象转型1

     1 //定义一种动物
     2 class Animal {
     3     public String name;
     4     Animal(String name) {
     5         this.name = name;
     6     }
     7 }
     8 //猫继承了动物
     9 class Cat extends Animal {
    10     public String eyeColor;
    11     Cat(String n, String e) {
    12         super(n);
    13         eyeColor = e;
    14     }
    15 }
    16 //狗继承了动物
    17 class Dog extends Animal {
    18     public String furColor;
    19     Dog(String n, String f) {
    20         super(n);
    21         furColor = f;
    22     }
    23 }
    24 
    25 public class Test {
    26     public static void main(String[] args) {
    27         Animal a = new Animal("name");
    28         Cat c = new Cat("catname", "blue");
    29         Dog d = new Dog("dogname", "black");
    30 
    31         System.out.println(a instanceof Animal);//true,a是一种Animal
    32         System.out.println(c instanceof Animal);//true,c是一种Animal
    33         System.out.println(d instanceof Animal);//true,d是一种Animal
    34         System.out.println(a instanceof Cat);//false  ,Animal不是一种Cat
    35 
    36         /*
    39      Animal类型的a指向子类对象Dog,
           a指向了Dog中Animal的name属性和Dog的furColor属性
           但是a只能访问Dog自身的name属性,不能访问Dog自身的furColor属性
    40 */ 41 a = new Dog("bigDog", "yellow"); 42 System.out.println(a.name);//bigDog 43 //System.out.println(a.eyeColor); errer 44 45 /* 46       a = new Dog后,a是一种Dog,只是不能访问子类特有的成员 47 */ 48 System.out.println(a instanceof Animal);//true 49 System.out.println(a instanceof Dog);//true 50 /* 51       要想访问子类特有的成员,需要强制类型转换 52 */ 53 Dog d1 = (Dog)a; 54 System.out.println(d1.furColor);//yellow 55 } 56 }

    对象转型2

     1 //定义一种动物
     2 class Animal {
     3     public String name;
     4     Animal(String name) {
     5         this.name = name;
     6     }
     7 }
     8 //猫继承了动物
     9 class Cat extends Animal {
    10     public String eyeColor;
    11     Cat(String n, String e) {
    12         super(n);
    13         eyeColor = e;
    14     }
    15 }
    16 //狗继承了动物
    17 class Dog extends Animal {
    18     public String furColor;
    19     Dog(String n, String f) {
    20         super(n);
    21         furColor = f;
    22     }
    23 }
    24 
    25 public class Test {
    26     public static void main(String[] args){
    27         Test test = new Test();
    28         Aniaml a = new Animal("name");
    29         Cat c = new Cat("catname", "blue");
    30         Dog d = new Dog("dogname", "black");
    31         test.f(a);
    32         test.f(c);
    33         test.f(d);
    34     }
    35     /*
    36     
    37     
    38     父类引用指向子类对象,可扩展性比较好(但是还没有达到最好,最好是多态39     否则需要写三个f()方法f(Animal a)、 f(Cat a) 、f(Dog a)
    40     才能调用有继承关系的Animal、Cat、Dog
    41     
    42     */
    43     public void f(Animal a) { //父类引用指向子类对象
    44         if(a instanceof Cat) {
    45             Cat cat = (Cat) a;
    46             System.out.println(" " + cat.eyesColor + " eyes");
    47         }else if(a instanceof Dog) {
    48             Dog dog = (Dog) d;
    49             System.out.println(" " + dog.eyesColor + " eyes");
    50         }
    51     }
    52 }

     4、动态绑定和多态(可扩展性达到最好)

    三个条件:

    • 要有继承
    • 要有重写
    • 父类引用指向子类对象

    动态绑定是指“在执行期间(而非编译期间)判断所引用对象的实际类型,根据实际的类型调用其相应的方法”

     1 class Animal {
     2     private String name;
     3     Animal(String name) {
     4         this.name = name;
     5     }
     6     public void enjoy(){
     7         System.out.println("叫声.....");
     8     }
     9 }
    10 
    11 class Cat extends Animal {
    12     private String eyeColor;
    13     Cat(String name, String eyeColor) {
    14         super(name);
    15         this.eyeColor = eyeColor;
    16     }
    17     public void enjoy() {
    18         System.out.println("猫叫声.....");
    19     };
    20 }
    21 
    22 class Dog extends Animal {
    23     private String furColor;
    24     Dog(String name, String furColor) {
    25         super(name);
    26         this.furColor = furColor;
    27     }
    28      public void enjoy() {
    29         System.out.println("狗叫声.....");
    30     };
    31 }
    32 
    33 class Lady {
    34     private String name;
    35     private Animal pet;
    36     Lady(String name, Animal pet) {
    37         this.name = name;
    38         this.pet = pet;
    39     }
    40     public void myPetEnjoy() {
    41         pet.enjoy();
    42     }
    43 }
    44 
    45 public class Test {
    46     public static void main(String[] args) {
    47         Cat c = new Cat("catname", "blue");
    48         Dog d = new Dog("dogname", "black");
    49 
    50         Lady l1 = new Lady("l1", c);
    51         Lady l2 = new Lady("l2", d);
    52 
    53         l1.myPetEnjoy();
    54         l2.myPetEnjoy();
    55     }
    56 }

    程序中有3个enjoy方法,当你调用的时候调的哪一个是根据实际当中的类型来确定,而不是通过引用中的类型来确定(参考对象上面对象转型)

     5、抽象类和方法

    用abstract关键字来修饰一个类时,这个类叫做抽象类

    用abstract关键字来修饰一个方法时,这个方法叫做抽象方法

    含有抽象方法的类必须被声明为抽象类

    抽象类必须被继承,抽象方法必须被实现

    抽象类不能被实例化

    抽象方法只需声明,不需要实现

    6、final关键字

    final的变量的值不能够被改变

    • final的成员变量
    • final的局部变量

    final的方法不能被重写

    final的类不能被继承

    7、接口

    接口是抽象方法和常量值的定义的集合

    从本质上讲,接口是一种特殊的抽象类,这种抽象类只包含常量和方法的定义,而没有变量和方法的实现

    接口定义举例:

    public interface Runner {
        public static final int id = 1;
        public void start();
        public void run();
        public void stop();
    }

     接口特性:

    接口可以多重实现

    接口中声明的属性默认为public static final的,也只能是public static final的

    接口中只能定义抽象方法,而且这些方法默认为public的,也只能是public的

    接口可以继承其它的接口,并添加新的属性和抽象方法

    多个无关的类可以实现同一接口

    一个类可以实现多个无关的接口

    与继承关系类似,接口与实现类之间存在多态性

     1 interface Singer {
     2     public void sing();
     3     public void sleep();
     4 }
     5 
     6 interface Painter {
     7     public void paint();
     8     public void eat();
     9 }
    10 
    11 class Student implements Singer {//Student继承了Singer
    12     private String name;
    13     Student(String name) {
    14         this.name = name;
    15     }
    16     public void study() {
    17         System.out.println("studying");
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void sing() {//实现了sing方法
    23         System.out.println("student is singing");
    24     }
    25     public void sleep() {//实现了sleep方法
    26         System.out.println("student is sleeping");
    27     }
    28 }
    29 
    30 class Teacher implements Singer, Painter {//Teacher继承了Singer、Painter
    31     private String name;
    32     public String getString() {
    33         return name;
    34     }
    35     Teacher(String name) {
    36         this.name = name;
    37     }
    38     public void teach() {
    39         System.out.println("teaching");
    40     }
    41     public void sing() {//Teacher继承了sing方法
    42         System.out.println("teacher is singing");
    43     }
    44     public void sleep() {//Teacher继承了sleep方法
    45         System.out.println("teacher is sleep");
    46     }
    47     public void paint() {//Teacher继承了paint方法
    48         System.out.println("teacher is paint");
    49     }
    50     public void eat() {//Teacher继承了eat方法
    51         System.out.println("teacher is eat");
    52     }
    53 }
    54 
    55 public class Test {
    56     public static void main(String[] args) {
    57         Singer s1 = new Student("le");
    58         s1.sing();
    59         s1.sleep();
    60         Singer s2 = new Teacher("steven");
    61         s2.sing();
    62         s2.sleep();
    63         Painter p1 = (Painter) s2;
    64         p1.paint();
    65         p1.eat();
    66     }
    67 }

    结果:
    student is singing
    student is sleeping
    teacher is singing
    teacher is sleep
    teacher is paint
    teacher is eat

  • 相关阅读:
    微信登录时用户信息无法写入数据库
    首次配置路由出现404的问题
    php登录模块出现循环重定向的问题
    关于动态生成dom绑定事件的问题
    将博客搬至CSDN
    C++ this指针的详解
    zigbee 中ZDO的理解
    zigbee端口的理解
    c语言的一些易错知识积累
    zigbee 安全通信加密链接密钥
  • 原文地址:https://www.cnblogs.com/wmjlh/p/7237077.html
Copyright © 2011-2022 走看看