Java面向对象-类与对象/构造器/封装继承多态/方法重写/super&this关键字

1.面向对象&面向过程

面向过程思想:
步骤清晰简单,第一步做什么,第二步做什么…
面向过程适合处理一些较为简单的问题
面向对象思想:
分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考,最后,才对某个分类下的细节进行面向过程的思索
面向对象适合处理复杂的问题,适合处理多人协作的问题
对于描述复杂的事物,为了从宏观上把握,从整体上分析,需要使用面向对象的思路来分析整个系统,具体到微观操作,仍然需要面向过程的思路去处理
面向对象编程的本质是:
以类的方式组织代码,以对象的方式封装数据
三大特性:封装,继承,多态

2.方法定义&方法调用

https://blog.csdn.net/m0_44959626/article/details/113417301

public class Demo01 {
    public static void main(String[] args) {
        //实例化Student类
        //对象类型 对象名=对象值
        Student student = new Student();
        student.say();
    }
}
public class Student {
    //非静态方法
    public void say(){
        System.out.println("hello");
    }
}
public class Demo02 {
    public static void main(String[] args) {
        //值传递
        int a=1;
        System.out.println(a);//1
        Demo02.change(a);
        System.out.println(a);//1
    }
    //返回值为空
    public static void change(int a){
        a=10;
    }
}
public class Demo03 {
    //引用传递
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        Demo03.change(person);
        System.out.println(person.name);//小明
    }
    public static void change(Person person){

        person.name="小明";
    }
    static class Person{
        String name; //null
    }
}

3.类与对象的关系

类是一种抽象的数据类型,它是对某一类事物整体的描述/定义,但并不能代表某一个具体的事物
对象是抽象概念的具体实例

4.创建与初始化对象

使用new关键字创建对象
除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用
类中的构造器也称为构造方法,在进行创建对象时必须调用的
特点:
必须和类的名字相同
必须没有返回类型,不能写void

public class Application {
    public static void main(String[] args) {
        //类,抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Student类的具体实例
        Student student = new Student();
        student.name="xiaoming";
        student.age=8;
        System.out.println(student.name);
        System.out.println(student.age);
    }
}
public class Student {
    String name;
    int age;
    public void study(){
        System.out.println(this.name+"在学习");
    }
}

5.构造器

public class Person {
    //一个;类即使什么都不写,也会存在一个方法
    String name;
    int age;
    //实例化初始值
    //使用new关键字,本质是在调用构造器
    public Person(){
        this.name="xiaoming";
    }
    //有参构造:一旦定义了有参构造,无参就必须显示定义
    public Person(String name){
        this.name=name;
    }
    public Person(int age) {
        this.age = age;
    }
    
}
public class Application {
    public static void main(String[] args) {
        Person person = new Person();

        //Person person1 = new Person("xiaoming");

        System.out.println(person.name);
    }
}

6.封装

封装:

程序设计追求:高内聚,低耦合
高内聚就是类的内部数据操作细节自己完成,不允许外部干涉
低耦合是仅暴露少量的方法给外部使用
通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,称为信息隐藏
属性私有,get/set

public class Student {
    //属性私有
    private String name;
    private int id;
    private char sex;
    private int age;
    //快捷键 alt+insert 选择Setter and Getter
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 = 3;
        }else{
            this.age=age;
        }
    }
}

public class Application {
    public static void main(String[] args) {
        /*提高程序安全性,保护数据
        * 隐藏代码的实现细节
        * 统一接口
        * 提高系统可维护性*/
        Student student = new Student();
        student.setName("xiaoming");
        System.out.println(student.getName());
        student.setAge(666);
        System.out.println(student.getAge());
    }
}

7.继承

继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
extends: 扩展 子类是父类的扩展
Java类中只有单继承,没有多继承
继承是类与类之间的一种关系,除此之外,类与类之间还有依赖、组合、聚合等关系
继承关系的两个类,一个为子类(派生类),一个是父类(基类),子类继承父类,用关键字extends表示

public class Person {
    //所有的类都默认直接或间接继承Object类
    public int moneny=666;
    public void say(){
        System.out.println("hello");
    }
}
public class Student extends Person{

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

8.super关键字

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

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

}
public class Student extends Person{
    public Student() {
        //隐藏代码:调用了父类的无参构造
        super();//调用父类的构造器,必须要在子类构造器的第一行
        System.out.println("Student无参执行了");
    }

    private String name="xiaohong";
    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();
        this.print();
        super.print();
    }
    public void test2(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }

}

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test1();
        student.test2("小明");
    }
}
Person无参执行了
Student无参执行了
Student
Student
Person
小明
xiaohong
xiaoming

注意点:

  1. super调用父类的构造方法,必须在构造方法第一行
  2. super必须只能出现在子类的方法或构造方法中
  3. super和this不能同时调用构造方法
    相比于this:
    代表的对象不同:
    this:本身调用这个对象
    super:代表父类对象的引用
    前提:
    this:没有继承也可以使用
    super:只有在继承条件下才能使用
    构造方法:
    this:本类的构造
    super:父类的构造

9.方法的重写

需要有继承关系,子类重写父类的方法

  1. 方法名必须相同
  2. 参数列表必须相同
  3. 修饰符:范围可以扩大 但不能缩小 public>protected>default>private
  4. 抛出异常:可以缩小,但不能扩大
    为什么要重写
    父类的功能,子类不一定需要,或者不一定满足
public class A extends B{
    public static void test(){
        System.out.println("A=>test");
    }
    @Override
    public  void test1(){
        System.out.println("A=>test1");
    }
}

public class B {
    //重写是方法的重写,和属性无关
    public static void test(){
        System.out.println("B=>test");
    }
    public  void test1(){
        System.out.println("B=>test1");
    }
}
public class Application {
    public static void main(String[] args) {
        //静态方法:方法的调用只与左边定义的数据类型有关
        //非静态:重写
        A a = new A();
        a.test();
        //父类的引用指向子类
        B b=new A();
        b.test();
        a.test1();
        b.test1();
    }
}

结果:

A=>test
B=>test
A=>test1
A=>test1

10.多态

动态编译:可扩展性
同一方法可以根据发送对象的不同而采用多种不同的行为方式
一个对象的实际类型时确定的,但可以指向对象的引用的类型很多
多态存在的条件:
有继承关系
子类重写父类的方法
父类引用指向子类对象
多态是方法的多态,属性没有多态性

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

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("hello2");
    }
    public void eat(){
        System.out.println("hello3");
    }
}
public class Application {
    public static void main(String[] args) {
        //Student能调用的方法都是自己的或者继承父类的
        Student student1 = new Student();
        //Person类可以指向子类,但是不能调用子类独有的方法
        Person student2 = new Student();
        Object student3=new Student();

        student2.run();//子类重写了父类的方法,执行子类的方法
        student1.run();
        //对象能执行哪些方法,主要看对象左边的类型
        student1.eat();
        ((Student) student2).eat();//强制转换
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值