Day14-封装、继承、多态

封装、继承、多态

一.封装

package Demo;

//类     private私有
public class student {
    //属性私有
    //名字
    private String name;
    //学号
    private int id;
    //性别
    private char sex;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>120 || age<0){
            this.age=3;
        }else{
            this.age = age;
        }
    }
//提供一些可以操作这个属性的方法
    //提供一些public的set、get方法

    //get,获得这个数据
    public String getName(){
        return name;
    }
    //set,给这个数据设置值
    public void setName(String name){
        this.name=name;
    }

    //alt+insert自动生成get、set方法




    //学习()

    //睡觉()
}


package Demo;

public class Application {
    public static void main(String[] args) {
        student s1 = new student();

        String name = s1.getName();
        s1.setName("LiLiLiYH");
        System.out.println(s1.getName());//LiLiLiYH
        s1.setAge(999);//不合法的
        System.out.println(s1.getAge());
    }
}


二.继承

1.什么是继承

package Demo.demo05;

//人   父类
//在java中,所有的类都默认直接或间接继承object
public class Person /* entends Object */{
    private int money=1_0000;
    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public void say(){
        System.out.println("说了一句话");

    }
}


package Demo.demo05;

//学生是人   派生类/子类
//子类继承了父类,就会有父类的全部方法
public class Student extends Person{

    //ctrl+h
    public static void main(String[] args) {
        Student student = new Student();
        student.say();//说了一句话
        System.out.println(student.getMoney());//10000
    }

}

java有四种权限修饰符:

项目 public protected default private
同一个类 yes yes yes yes
同一个包 yes yes yes no
子父类 yes yes no no
不同包 yes no no no

附:类间的方法的调用

如果不在一个类中,静态方法调用其他类中的的静态方法,必须通过:

类名.静态方法();

如果不在一个类中,静态方法调用其他类的非静态方法,需要导入该类的包。以及通过创建对象调用。


在不同类中,非静态方法调用其他类的静态方法,需要通过导入该类中的包,并且需要通过类名来调用。
在不同类中,非静态方法调用其他类的非静态方法时,需要导入该类中的包,还需要通过创建对象来调用。

2.Super详解

package Demo.demo05;

//人   父类
//在java中,所有的类都默认直接或间接继承object
public class Person /* entends Object */{
    public Person() {
        System.out.println("Person无参构造执行了");
    }

    protected String name="LiLiLiYH";
    public void print(){
        System.out.println("person");
    }
}


package Demo.demo05;

//学生是人   派生类/子类
//子类继承了父类,就会有父类的全部方法
public class Student extends Person{
    public Student() {
        //隐藏代码,调用了父类的无参构造
        super();//调用父类构造器,必须要在子类构造器的第一行
        System.out.println("Student无参构造执行了");
    }

    public void print(){
        System.out.println("student");
    }
    public void test2(){
        print();//student
        this.print();//student
        super.print();//person
    }
//    private String name="李";
//    public void test(String name){
//       System.out.println(name);//Li
//       System.out.println(this.name);//李
//       System.out.println(super.name);//LiLiLiyh
//   }


}


package Demo.demo05;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        //student.test("Li");
        student.test2();
    }
}

super注意点:

  1. super调用父类的构造方法,必须在构造方法的第一个
  2. super必须只能出现在子类的方法或构造方法中
  3. super和this不能同时调用构造方法

VS

this:

  • 代表的对象不同

    this:本身调用者这个对象

    super:代表父类对象的应yong

  • 前提

    this:没有继承也可以使用

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

  • 构造方法

    this:本类的构造

    super:父类的构造

CSDN上有个写的很好,这里推荐可以看下:super&this

3.方法重写

package Demo.demo05;

public class B {
    protected void test(){
        System.out.println("B>=test");
    }
}


package Demo.demo05;

public class A extends B{
    //Override重写
    @Override//注解:有功能的注释
    public void test() {
        System.out.println("A>=test");
    }
}


package Demo.demo05;

public class Application {

    //静态方法和非静态方法区别很大
    //静态方法:方法的调用只和左边定义的数据类型有关
    //非静态:重写
    public static void main(String[] args) {
        A a = new A();
        a.test();//A>=test

        //父类的引用指向了子类
        B b = new A();//子类重写了父类的方法
        b.test();
    }

}

三.多态

1.什么是多态

动态编译:类型:可拓展性

package Demo.demo05;

public class Person {
    public void run(){
        System.out.println("run");
    }

}


package Demo.demo05;

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}


package Demo.demo05;

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

        //可以指向的引用类型就不确定了:父类的引用指向子类

        //Student能调用的方法,都是自己的或者继承父类的
        Student s1 = new Student();
        //Person父类型,可以指向子类,但是不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        s2.run();//子类重写了父类的方法,执行子类的方法
        s1.run();

        //对象能执行哪些方法。主要看对象左边的类型,和右边的关系大不大
        s1.eat();
        //s2.eat();报错
        ((Student) s2).eat();//强转
    }
}


个人感觉初学听完狂神说的这节视频还是有点迷茫,另外又找了个视频可以看下

多态

这个逻辑感觉更清晰些


2.instanced和类型转换

instance of 判断一个对象是什么类型

先建立一个父类Person

再建立两个子类Teacher和Student

测试类中代码如下:

package Demo01;

public class Application {
    public static void main(String[] args) {
         Object object = new Student();
         //相当于   Student student1 = new Student();
         //        Object object=student1;

         //Object>String
         //Object>Person>Student
         //Object>Person>Teacher
         //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);//编译报错

    }
}

类型转换


父类

package Demo01;

public class Person {
    public void run(){
        System.out.println("run");
    }
}

子类

package Demo01;

public class Student extends Person {
    public void go(){
        System.out.println("go");
    }
}

测试类

package Demo01;

public class Application {
    public static void main(String[] args) {
        //类型之间的转化:低转高自动转,高转低强转

        //高                低
        //子类转成父类,可能丢失自己的本来的一些方法
        Person s = new Student();

        //现在s是Person类型,s.go();会报错
        //将s转换为Student类型,我们就可以使用Student类型的方法了
        Student s1=(Student)s;
        s1.go();
        //把上面两句话写成一句话((Student) s).go();

    }
}

posted @ 2022-03-31 21:00  LiLiLiYH  Views(41)  Comments(0)    收藏  举报