Java学习Day06 封装 继承 多态

Java学习Day06 封装 继承 多态

封装

/*
* 1提高程序安全性 保护数据
* 2隐藏代码
* 3统一结构
* 4系统可维护性增加
* */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
//      s1.name = "a";
//      System.out.println(s1.name);
        //如果用private 就会报错不能访问
        s1.setName("御狐王");
        System.out.println(s1.getName());
        s1.setAge(100);
        System.out.println(s1.getAge());

    }
}
//封装 思想 高内聚低耦合 类的内部操作自己完成 仅暴露少量方法给外部使用
//代码 属性私有 get/set
public class Student {
    //私密
    private String name;
    private int id;
    private char gender;

    private int age;

    public int getAge() {

        return age;
    }

    public void setAge(int age) {
        if (age > 120 || age < 1) {
            this.age = 3;
        } else {
            this.age = age;
        }
    }
    //有操作private 属性的方法 get set
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
    //alt + insert 自动生成getter setter
}

继承

//继承
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        //子类继承父类的所有方法以及数值
        //当然不能继承private
        //default
        //protected
        student.say();
        System.out.println(student.getMoney());

    }

}
//父类
//类与类的关系除了继承还有依赖 组合 聚合
public class Person {
    public void say(){
        System.out.println("say one thing");
    }
    private int money = 10_000_000;

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
//子类 派生类
public class Student extends Person{
    //Person person;组合
    //Ctrl + H 打开继承树
    //在java中所有的类都直接或间接继承object类
}
//子类 派生类
public class Teacher extends Person{
}

super

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

        student.test("yyyhuw");
        student.test1();
    }
}
public class Person {
    protected String name = "御狐王";

    public Person() {
        System.out.println("Person无参");

    }

    public void print(){
        System.out.println("Person");
    }
}
/*super
* 1 super调用父类的构造方法 必须在子类第一行
* 2 super 必须只能出现在子类的方法或构造器中
* 3 super 和this不能同时调用相同的构造方法
*
* this
* 1 本身调用者的对象
* 2 没有继承也能用
* 3 调用本类构造
*
* */
public class Student extends Person{
    private String name ="yuhuw";

    public Student() {
        //这是隐藏代码不写也有:super();这是默认调用的
        super();//调用父类的无参构造器 必须在子类构造器的第一行
        //你可以直接调用父类的有参 如果不写直接默认调用无参
        //直接调用无参 若父类是无参正常调用 若父类写了有参 会替换无参导致代码出错
        System.out.println("Student无参");
    }

    public void print(){
        System.out.println("Student");
    }
    public void test(String name){
        System.out.println(name);//输出yyyhuw
        System.out.println(this.name);//输出当前类
        System.out.println(super.name);//输出父类 当然如果是private不能调用

    }
    public void test1(){
        print();//不能明确print用的是父类还是子类
        this.print();//用自己的类
        super.print();//用父类 同上
    }
}

方法重写

//方法重写
public class Application {
    public static void main(String[] args) {
        //静态方法的调用只和 定义的数据类型有关
        A a= new A();
        a.test();//A类

        B b= new A();//父类的引用指向了子类
        b.test();
        //静态方法和非静态方法不同
        //只有非静态中 子类重写了父类的方法
    }
}
/*
* 重写
* 1需要继承关系 且非静态关系子类重写父类
* 2方法名相同方法体不同
* 3参数列表相同
* 4修饰符范围可以扩大不能缩小  public protected default private
* 5抛出的异常可以缩小不能扩大
* 为什么要重写
* 父类功能子类不一定需要.满足
*
* */
public class A extends B{
    //方法重写 alt + insert Override
    @Override
    public void test() {
        System.out.println("A");
    }
    //    public void test(){
//        System.out.println("A");
//    }
}
public class B {
    public void test(){
        System.out.println("B");
    }

}

多态

public class Application {
    public static void main(String[] args) {
        //new Person();一个对象的实际类型是确定的
        //子类调用的方法只能调用自己的或者继承父类的 也就是子类可以调用自己独有的方法
        Student s1 = new Student();
        //父类可以指向子类不能调用子类独有的方法 也就是父类没有的子类有不能调用
        Person s2 = new Student();//父类的引用指向子类
        Object s3 = new Student();

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

    }
}
//多态 是方法的多态
//父类和子类的联系 类型转换异常 ClassCastException
//存在条件 继承关系 方法重写
//父类的引用指向的是子类对象
//3种没有多态的
//static 方法属于类 不属于实例
//final   常量
//private 私有
//instanceof 引用类型转换
public class Person {
    public void run(){
        System.out.println("run");

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

类型转换

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

        Object object = new Student();
        //Object Person Student
        //Object Person Teacher
        //Object String
        // X instanceof Y 能不能编译通过 是取决于对象类型之间有无父子关系
        //输出true false 是new 类型之间是否有父子关系
        System.out.println(object instanceof Student);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Object);
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);
        System.out.println("=====================");
        Person person = new Student();
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Teacher);
        //System.out.println(person instanceof String);编译报错
        System.out.println("=====================");
        Student student = new Student();
        System.out.println(student instanceof Student);
        System.out.println(student instanceof Person);
        System.out.println(student instanceof Object);
        //System.out.println(student instanceof Teacher);编译报错
        //System.out.println(student instanceof String);编译报错

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

}
public class Teacher extends Person{


}

static

public class Person {
    //2
    {
        System.out.println("匿名代码块");
        //匿名代码块
    }
    //1
    //最早执行 和类一起加载 且只执行一次
    static {
        System.out.println("静态代码块");
        //静态代码块
    }
    //3
    public Person() {
        System.out.println("构造器");
    }

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("===============");
        Person person1 = new Person();
    }

}
public class Student {
    private static int age;//静态变量  多线程
    private double score;//非静态变量

    public void run(){

    }
    public static void go(){


    }
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);
        System.out.println(s1.age);
        System.out.println(s1.score);

    }
}
//import java.lang.Math.random;报错
import static java.lang.Math.random;//静态导入包
public class Test {

    public static void main(String[] args) {
        System.out.println(random());

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值