java学习记录10-三大特征(封装,继承,多态)

一、封装

Appliction.java

public class Appliction {
    public static void main(String[] args) {
        Person ding = new Person();
        ding.setName("ding");
        ding.setAge(121);
        System.out.println(ding.getName());
        System.out.println(ding.getAge());
    }
}

Person.java

public class Person {
//    private:私有
    private String name;
    private int age;
//    封装
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age>120 || age<0) {
            this.age = 1;
        }else{
            this.age = age;
        }
    }
}

二、继承

1.object类

Application.java

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.name = "继承名字";
        student.say();
        System.out.println(student.name);
    }
}

父类Person:

public class Person {
    public String name;
    public void say(){
        System.out.println("我是父亲方法");
    }
}

子类Student :

public class Student extends Person{
//    ctrl+h => 查看继承树
}


2.super

Application.java

public class Application {
    public static void main(String[] args) {
       Student student = new Student();
       student.test("传参name");
    }
}

父类Person:

public class Person {
   public Person() {
        System.out.println("父类构造器执行");
    }
    public String name = "父类name";
}

子类Student :

public class Student extends Person{
//    ctrl+h => 查看继承树
	public Student() {
        // 1.即使不写 也会默认执行 父类构造器
        // 2.要写的话 super也只能写在第一个
        super();
        System.out.println("子类构造器执行");
    }
    public String name = "子类name";
    public void test(String name){
        System.out.println(name);       // 传参name
        System.out.println(this.name);  // 子类name
        System.out.println(super.name); // 父类name
    }
}


3.方法重写

Application.java

public class Application {
    public static void main(String[] args) {
        A a = new B();
        B b = new B();
        a.noStatic();  // B-noStatic
        b.noStatic();  // B-noStatic
        a.hasStatic(); // A-noStatic
        b.hasStatic(); // B-noStatic

//        静态方法(static):方法的调用只和左边,定义的数据类型有关
//        非静态方法:   重写
//        重写
//            1.方法名必须相同
//            2.参数名必须相同
//            3.修饰符,可以扩大但是不能缩小 public>private
//            4.抛出异常:可以被缩小,但是不能扩大
    }
}

父类A:

public class A{
    public void noStatic(){
        System.out.println("A-noStatic");
    }
    public static void hasStatic(){
        System.out.println("A-hasStatic");
    }
}

子类B :

public class B extends A{
    public void noStatic(){
        System.out.println("B-noStatic");
    }
    public static void hasStatic(){
        System.out.println("B-hasStatic");
    }
}

三、多态

Application.java

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();

        s1.say(); // Student
        s2.say(); // Student

        s1.studentSay(); // studentSay
//        s2.studentSay(); 报错
//        强制转换
        ((Student) s2).studentSay(); // studentSay

//        多态
//        对象向能执行哪些方法,只要看左边的类型,和右边关系不大
//        例:Person s2 = new Student();  s2只能执行Person里有的方法
    }
}

父类Person:

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

子类Student :

public class Student extends Person {
    public void say(){
        System.out.println("Student");
    }
    public void studentSay(){
        System.out.println("studentSay");
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值