2021-07-22

封装

要义

该展示的展示,该隐藏的隐藏。

设计程序的时候要追求“高内聚,低耦合”。高内聚指的是:类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。

封装(数据的隐藏)

通常,应该禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。

重点

属性私有,get/set

代码

package com.oop;
import com.oop.demo04.Student;
/*
1. 提高程序的安全性,保护数据
2. 隐藏代码的实现细节
3. 统一接口
4. 系统的可维护性
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

     s1.setName("jiajia");
     //判断一个类是不是相同:1.方法名是否相同 2. 参数列表是否相同
        System.out.println(s1.getName());
    }
}
/*
package com.oop.demo04;
//类   private:私有的
public class Student {
    //属性私有
    private String name; //名字
    private int id;//学号
    private char sex; //性别
    //提供一些可以操作这个属性的方法
    //提供一些public的get、set方法

    //get  获得这个数据
public String getName(){
    return this.name;
}
    //set  给这个数据设置值
    public void setName(String name){
       this.name = "jiajia";
    }
    //Alt + insert
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public char getSex() { return sex; }
    public void setSex(char sex) { this.sex = sex; }
}
 */

继承

继承的本质

继承的本质是堆某一批类的抽象,从而实现对现实世界更好的建模。

extends

extends的意思是“扩展”。子类是父类的扩展。

特点

JAVA中类只有单继承,没有多继承!
即:你只能有一个爸爸,但是你的爸爸可以有好多个儿子。

解释

  1. 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
  2. 继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
  3. 子类和父类之间,从意义上将应该具有“is a”的关系

代码

package com.oop;
import com.oop.demo05.Student;
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
        student.setMoney(1000000000);
    }
}
/*
package com.oop.demo05;
//Person 人 :父类/基类
public class Person {
    //public
    //private  子类就用不了了
    //不写会默认的  default
    //protected  受保护的
    public void say(){
        System.out.println("说了一句话");
    }
    private int money = 1000000000;
    public int getMoney(){
        return money;
    }
    public void setMoney(int money){
        this.money = money;
        System.out.println(money);
    }
}
package com.oop.demo05;
//学生  is  人  子类/派生类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
    //Ctrl + H  出现继承树
}
 */

super

私有的无法被继承,用private无法继承

代码

package com.oop;

import com.oop.demo05.Person;
import com.oop.demo05.Student;

public class Application {

    public static void main(String[] args) {
       Student student = new Student();
       /*student.test("佳佳");*/
        student.test1();
    }
}
/*
public class Student extends Person {
    private String name = "aichiyu";

    public void test(String name){
        System.out.println(name);//佳佳
        System.out.println(super.name);//jiajia
        System.out.println(this.name);//aichiyu
    }
    public Student() {
        //有个隐藏代码:调用了父类的无参构造
        super();//调用父类的构造器,必须要在子类构造器的第一行
        System.out.println("Student无参执行了");
    }

    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
}
public class Person extends Object {
protected String name = "jiajia";

public Person() {
        System.out.println("Person无参执行了");
        }
public void print(){
        System.out.println("Person");
        }
}*/

note

super注意点:
    1.super掉以哦那个父类的构造方法,必须在构造方法的第一个
    2.super 必须只能出现在子类的方法或者构造方法中!
    3.superthis 不能同时调用构造方法

Vs this:
    代表的对象不同
        this: 本身调用者这个对象
        super: 代表父类对象的应用
    前提
        this: 没有继承也可以使用
        super: 只能在继承条件下才可以使用
    构造方法
        this() ;本类的构造
        super(): 父类的构造!

重写

重写都是方法的重写,和属性无关
静态的方法和非静态的方法区别很大
静态是对类,非静态是对对象

代码

package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
    public static void main(String[] args) {
       //静态方法   //方法的调用只和左边,定义的数据类型有关
        //非静态: 可以重写
        A a = new A();
        a.test();//A
        //父类的引用指向了子类
        B b = new A();//子类重写了父类的方法
        b.test();//B
    }
}
/*
public class A extends B {
    public void test(){
        System.out.println("A=>test()");
    }
}
//重写都是方法的重写,和属性无关
public class B {
    public void test(){
        System.out.println("B=>test()");
    }
}
 */

note

重写:需要有继承关系,子类重写父类的方法!
    1. 方法名必须相同
    2. 参数列表必须相同
    3. 修饰符:范围可以扩大,但是不可以缩小:public > Protected > Default > private
    4. 抛出的异常:范围可以被缩小,但不能扩大: ClassNotFoundException--->Exception()

重写,子类的方法和父类必须要一致: 方法体不同

为什么需要重写:
    1. 父类的功能,子类不一定需要,或者不一定满足!
      ALT + insert override 重写

多态

解释

即同以方法可以根据发送对象的不同而采用多种不同的行为方式。

一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多。

存在的条件

  1. 有继承关系。
  2. 子类重写父类方法。
  3. 父类引用指向子类。

注意

多态是方法的多态,属性没有多态性。

代码

package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student();
        //new Person()'

        //可以指向的引用类型就不确定了:父类的引用指向子类
        //子类能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //父类型,可以指向子类,但是不能调用子类独有的方法
        Person s2 = new Student();//父类的引用指向子类
        Object s3 = new Student();
        s2.run();//子类重写了父类的方法,执行子类的方法
        s1.run();

        //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
        s1.play();
        ((Student) s2).play();
    }
}
/*
package com.oop.demo06;
public class Person {
    public void run(){
        System.out.println("run");
    }
}
多态注意事项:
1. 多态是方法的多态,属性没有多态
2. 父类和子类,有联系  类型转换异常! ClassCastException!
3. 存在条件:继承关系,方法需要重写,父类引用指向子类对象!
   不可以重写的:
     1. static 方法:属于类,它不属于实例
     2. final 常量;
     3. private 方法;
package com.oop.demo06;
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("girl");
    }
    public void play(){
        System.out.println("play");
    }
}
 */

instanceof 和 类型转换

代码

package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
    public static void main(String[] args) {

        //Object > Person > Student
        //Object > Person > Person
        //Object > String
        Object object = new Student();

        //System.out.println(X instanceof Y);  能不能编译通过!  有关系就可以通过

        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        System.out.println("--------------------------------");
        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);编译报错!
        System.out.println("--------------------------------");
        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);编译报错
        //System.out.println(student instanceof String);编译报错
    }
}
/*
public class Person {}
public class Teacher extends Person {}
public class Student extends Person{}
*/
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
    public static void main(String[] args) {
        //类型之间的转化:  基本类型转化   高低64  32  16  8   父  子

        //高                   低
        Person obj = new Student();
        //student将这个对象转换为Student类型,就可以使用Student类型的方法了!
        Student student = (Student) obj;
        student.go();
        ((Student) obj).go();

        //子类转换为父类,可能丢失自己本来的一些方法
        Student student1 = new Student();
        student.go();
        Person pewson = student1;
    }
}
/*
1.父类引用指向子类的对象:  Person pewson 指向 student;
2.把子类转换为父类,向上转型;可以直接转,无需强制转型
3.吧父类转换为子类,想下转型;要强制转型, 可能会丢失子类一些独有的方法
4.方便方法的调用,减少重复的代码! 更简洁

抽象的


package com.oop.demo06;
public class Student extends Person{
   public void go(){
       System.out.println();
   }
}
 */

小结

  1. 父类引用指向子类的对象: Person pewson 指向 student;
  2. 2.把子类转换为父类,向上转型;可以直接转,无需强制转型
  3. 吧父类转换为子类,想下转型;要强制转型, 可能会丢失子类一些独有的方法
  4. 方便方法的调用,减少重复的代码! 更简洁
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值