Java三大要点

Java三大要点

------封装、继承、多态



 

封装

 1 /*
 2     1.提高程序的安全性,保护数据
 3     2.隐藏代码的实现细节
 4     3.统一接口
 5     4.系统可维护增加
 6  */
 7  8 public class Private {
 9 10     //私有属性(封装)
11     private String name;
12     private int age;
13     private int toll;
14 15 16     public String getName() {
17         return name;
18     }
19 20     public void setName(String name) {
21         this.name = name;
22     }
23 24     public int getAge() {
25         return age;
26     }
27 28     //防止数据不合法破坏程序
29     public void setAge(int age) {
30         if (age >=120 || age <=0) {
31            this.age = 3;
32         } else {
33             this.age = age;
34         }
35     }
36 37     public int getToll() {
38         return toll;
39     }
40 41     public void setToll(int toll) {
42         this.toll = toll;
43     }
44 }

 

 

继承(extends)

继承就是扩展,子类是父类的扩展。

Java中只有单继承,没有多继承。(一个父类可以有多个子类,一个子类只能有一个父类)

 1 //Java中所有的类都默认直接继承object类
 2 //Animal动物 父类,基类
 3 public class Animal {
 4  5     public Animal() {
 6         System.out.println("Animal被调用");
 7     }
 8     protected String x = "90";
 9 10     public void say() {
11         System.out.println("say one");
12     }
13 }
14 15 ==============================================================================
16 ==============================================================================
17     
18 //Person人 子类,派生类 (继承父类的所有方法和属性,私有和保护的无法继承)
19 //ctrl + h 查看继承的层次结构
20 public class Person extends Animal {
21 22     //子类构造器会隐式调用父类无参构造器
23     public Person() {
24         super();  //调用构造器必须写在构造器第一行
25         System.out.println("Person被调用");
26     }
27 28 }
29 30 ==============================================================================
31 ==============================================================================
32 33 //Man男人 子类,派生类
34 public class Man extends Person {
35 36 }

 

  • super注意点

    1. 调用父类的构造器必须在构造方法的第一句。

    2. 必须只能出现在子类的放法或构造方法中。

    3. super和this不能同时调用构造方法。

  • this&&super代表的对象不同:

    >>this

    ·本身调用者这个对象

    ·没有继承也可以使用

    ·this()本类的构造

    >>super

    ·代表父类对象的应用

    ·只有在继承条件才可以使用

    ·super()父类的构造

重写方法(overrides method)(要在非静态下重写,只能是public)

重写:需要有继承关系,子类重写父类的方法(重写是子类的方法和父类必须一致,方法体不同)

1. 方法名必须相同
2. 参数列表必须相同
3. 修饰符范围可以扩大不能缩小
4. 抛出的异常范围可以缩小不能扩大

 

无法重写的情况

    1. static 方法是属于类,不属于实例
2. final 常量
3. private

 

 1 public class Animal {
 2  3       protected String x = "90";
 4  5     public void say() {
 6         System.out.println("say one");
 7     }
 8 }
 9 10 ==============================================================================
11 ==============================================================================
12 13 public class Person extends Animal {    
14     //重写方法不重写属性
15     public void say() {
16         System.out.println("say two");
17     }
18 }
19 Person r = new Person();
20 Animal z = new Person();  //父类类型可以引用子类
21 Animal x = new Animal();
22 23 r.say();  //输出:say two
24 z.say();  //输出:say two
25 x.say();  //输出:say one

 

多态(建立在方法重写上)

多态存在条件

    * 有继承关系
* 子类重写父类方法
* 父类引用指向子类对象
Object object = new Person();

 

 多态注意事项

    1. 多态是方法的多态,属性没有多态
2. 父类和子类,需要有联系   类型转换异常!ClassCastException!
3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象   Father f1 = new Son();

 

instanceof和类型转换

判断一个对象是什么类是否有父子关系 格式:对象 instanceof 类

 1 //Animal父类
 2 //Person||Dog 继承Animal
 3 //Man||Woman 继承Person
 4  5 Object object1 = new Object();
 6 Object object2 = new Person();
 7  8 System.out.println(object1 instanceof Animal); //false
 9 System.out.println(object2 instanceof Dog);    //false
10 System.out.println(object2 instanceof Object); //true
11 System.out.println(object2 instanceof Animal); //true
12 13 //System.out.println(person1 instanceof Dog);
14 //System.out.println(person1 instanceof String);  //毫无关系就会报错

<p1>类型转换<p1>

  1. 父类引用指向子类对象;

  2. 把子类转换为父类,(向上转型)丢失方法;

  3. 把父类转换为子列,(向下转型)强制转换;

  4. 存在意义:方便方法调用,减少重复的代码。

 1 //Animal父类
 2 //Person||Dog 继承Animal
 3 //Man||Woman 继承Person
 4  5 Man man = new Man();
 6  7 //子类转换为父(小-->大),但可能会丢失自己本来的一些方法
 8 Animal animal = man;
 9 Object object = man;
10 11 //父类转换为子类(大-->小)需要强制类型转换
12 Person person = (Person) Animal;

 

课外题

 1 class A {
 2     public String show(D obj) {
 3         return ("A and D");
 4     }
 5  6     public String show(A obj) {
 7         return ("A and A");
 8     }
 9 10 }
11 12 class B extends A{
13     public String show(B obj){
14         return ("B and B");
15     }
16 17     public String show(A obj){
18         return ("B and A");
19     }
20 }
21 22 class C extends B{
23 24 }
25 26 class D extends B{
27 28 }
29 30 public class Demo {
31     public static void main(String[] args) {
32         A a1 = new A();
33         A a2 = new B();
34         B b = new B();
35         C c = new C();
36         D d = new D();
37 38         System.out.println("1--" + a1.show(b));
39         System.out.println("2--" + a1.show(c));
40         System.out.println("3--" + a1.show(d));
41         System.out.println("4--" + a2.show(b));
42         System.out.println("5--" + a2.show(c));
43         System.out.println("6--" + a2.show(d));
44         System.out.println("7--" + b.show(b));
45         System.out.println("8--" + b.show(c));
46         System.out.println("9--" + b.show(d));
47     }
48 }

 当父类对象引用变量引用子类对象时,被引用对象的类型决定了调用谁的成员方法,引用变量类型决定可调用的方法。如果子类中没有覆盖该方法,那么会去父类中寻找。

 

继承链中对象方法的调用的优先级:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O) 

 1 class X {
 2     public void show(Y y){
 3         System.out.println("x and y");
 4     }
 5  6     public void show(){
 7         System.out.println("only x");
 8     }
 9 }
10 11 class Y extends X {
12     public void show(Y y){
13         System.out.println("y and y");
14     }
15     public void show(int i){
16 17     }
18 }
19 20 class Demo{
21     public static void main(String[] args) {
22         X x = new Y();
23         x.show(new Y());
24         x.show();
25     }
26 }

解释(explain):X是引用变量类型,它决定哪些方法可以调用;show()和 show(Y y) 可以调用,而 show(int i)不可以调用。Y 是被引用对象的类型,它决定了调用谁的方法:调用 y 的方法。

Y 继承了 X,覆盖了 X 中的 show(Y y) 方法,但是没有覆盖 show() 方法。这个时候,引用类型为X的 x 指向的对象为 Y,这个时候,调用的方法由 Y 决定,会先从 Y 中寻找。执行 x.show(new Y());,该方法在 Y 中定义了,所以执行的是 Y 里面的方法;但是执行 x.show(); 的时候,有的人会说,Y 中没有这个方法啊?它好像是去父类中找该方法了,因为调用了 X 中的方法。事实上,Y 类中是有 show() 方法的,这个方法继承自 X,只不过没有覆盖该方法,所以没有在 Y 中明确写出来而已,看起来像是调用了 X 中的方法,实际上调用的还是 Y 中的。

posted @ 2022-07-13 17:27  坚强的娃子  阅读(41)  评论(0)    收藏  举报