Day05 JAVA学习笔记之封装、继承

封装

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("jxr");
        System.out.println(s1.getName());//jxr

        s1.setAge(999);//不合法
        System.out.println(s1.getAge());

    }
}
//类 private:私有
public class Student {
    //属性私有:不能通过main方法进行修改和使用
    //名字
    private String name;
    //学号
    private int id;
    //性别
    private char sex;

    private int age;

    //提供一些可以操作这些属性的方法
    //提供一些public的get、set方法

    //get获得这个数据
    public String getName(){
        return this.name;
    }

    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;
    }

    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;
    }

    public int getAge() {
        return age;
    }

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

封装的意义:

1.提高程序的安全性,保护数据

2.隐藏代码的实现细节

3.统一接口

4.系统可维护增加了

继承

//老师 is 人 :派生类,子类 (使用extends继承)
public class Teacher extends Person {
}
public class Student{
    Person person;//这是组合的方法
}

子类还继承父类的所有方法

public class Student extends Person{
}

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
    }
}

Student类就可以使用Person类的方法,因为Student为Person类的子类。

在Java中都默认直接或者间接继承Object类。(一般是隐式继承))

Super详解

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

        Student student = new Student();
        student.test("123");
    }
}

//Person父类
public class Person {

    protected String name = "jxr";
}

public class Student extends Person{

    private String name = "abc";

    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}

输出

 super和this类似,不过是调用的父类的属性。

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

        Student student = new Student();
        student.test("123");
        student.test1();
    }
}

//Person父类
public class Person {

    protected String name = "jxr";

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

public class Student extends Person{

    private String name = "abc";

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

    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }

    public void test1(){
        print();
        this.print();
        super.print();
    }
}

输出

私有的private属性无法被继承 

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

        Student student = new Student();
    }
}

public class Person {

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

public class Student extends Person{

    public Student(){
        System.out.println("Student无参执行了");
    }
}

结果

 

 因为此处默认隐藏了一个super(),调用了父类,而且super()必须在第一行。this()也必须是第一行。

super()是调用父类的构造器,且父类构造器有参,super()也得有参。(默认无参)

super注意点:
1. super调用父类的构造方法,必须在构造方法的第一个

2. super 必须只能出现在子类的方法或者构造方法中!

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

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

构造方法
this():本类的构造

super():父类的构造

方法的重写 

public class Application {
    public static void main(String[] args) {
        A a = new A();
        a.test();

        B b = new A();
        b.test();
    }
}
public class A extends B{

    public static void test(){
        System.out.println("A=>test()");
    }

}
//重写都是方法的重写,和属性无关
public class B {

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

重写都是方法的重写,和属性无关

此处输出

 因为父类的引用指向了子类

B b = new A(); B指向的是A

当把A和B的static修饰符去掉之后,就出现了如图的o符号。即代表重载 

alt + insert 重写方法 

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

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

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

}

//重写都是方法的重写,和属性无关
public class B {

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

结果为

 静态方法和非静态方法的区别很大

//静态方法: //方法的调用只和左边,即定义的数据类型有关
//非静态方法:可以重写

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

重写,子类的方法和父类必要一致;方法体不同!
为什么需要重写:
父类的功能,子类不一定需要,或者不一定满足!
Alt + Insert:override;
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值