07-14_04封装、继承、多态

封装

package com.oop.demo04;
//封装类
//类    private:  私有
public class Student {
    //属性私有
    private String name; //名字
    private int id;      //学号
    private char sex;    //性别
    private int age;     //年龄
    //提供一些可以操作这个属性的方法!
    //提供一些public的get、set方法

    //get 获得这个数据
    public String getName() {
        return 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<0 || age>120){//不合法
            this.age = 3;
        }else{

            this.age = age;
        }

    }
}





/*
封装:
    1。提高程序的安全性,保护数据
    2。隐藏代码的实现细节
    3。统一接口
    4。系统可维护增加了


//执行方法
public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("马克吐痰");
        System.out.println(s1.getName());

        s1.setAge(200);
        System.out.println(s1.getAge());
    }

*/

继承

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

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

JAVA中类只有单继承,没有多继承!

继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。

继承关系的俩个类,一个为子类(派生类).一个为父类(基类)。

子类继承父类,使用关键字extends来表示。子类和父类之间,从意义上讲应该具有"is a"的关系.

object类

super

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

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

Person();

package com.oop.demo05;

public class Person {

    public Person() {
        System.out.println("Person的无参构造");
    }

    public Person(String name) {
        this.name = name;
    }

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

Student();

package com.oop.demo05;


public class Student extends Person{

    public Student() {
        super("name");
        System.out.println("Student的无参构造");
    }

    private String name = "aiyinsidan";
    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);

    }

}

Application();

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

}

方法重写 重点—>多态

重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同

​ 2. 参数列表列表必须相同

​ 3.修饰符:范围可以扩为: public>Protected>Default>private

  1. 抛出的异常:范围,可以被缩小,但不能扩大;

    ClassNotFoundException–>Exception(大)

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

为什么需要重写:

  1. 父类的功能,子类不一定需要,或者不一定满足!

    Alt+Insert ; override;

多态

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

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

  • 多态存在的条件

    • 有继承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意:多态是方法的多态,属性没有多态性。

Person()

package com.oop.demo06;

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


/*
多态注意事项:
1. 多态是方法的多态,实行没有多态
2. 父类盒子类,有联系  类型转换异常! ClassCastException!
3. 存在条件:  继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();

    父类不可以引用
    1.static 方法,属于类,它不属于实例
    2.final  常量;
    3.private 方法;
; */

Student()

package com.oop.demo06;

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

    public void eat(){
        System.out.println("eat");
    }
}
  • 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 > String
        //Object > Person > Teacher
        //Object > Person > Student

//        System.out.println(X instanceof Y);//能不能编译通过!   接口

        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//ture
        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);//ture
        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);   //ture
        System.out.println(student instanceof Object);   //true
//        System.out.println(student instanceof Teacher);//编译报错!
//        System.out.println(student instanceof String); //编译报错!
    }
}

true
true
true
false

false


true
true
true

false


true
true
true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值