05面向对象

什么是面向对象

在这里插入图片描述
在这里插入图片描述
以类的方式组织代码,以对象的方式组织(封装)数据。

回顾方法的定义

在这里插入图片描述

值传递

package oop.demo01;

public class demo01 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//a=1
        num(a);
        System.out.println(a);//a=1
    }
    public static void num(int a){
        a = 10;
    }
}

引用传递:对象(本质还是值传递)

package oop.demo01;

public class demo02 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        demo02.cheng(person);
        System.out.println(person.name);//zhangf


    }

    public static void cheng(Person person){
        person.name = "zhangf";
    }
}
class Person {
    String name;//null
}

类与对象的创建

在这里插入图片描述

package oop.demo02;

public class student {
    String name;
    int age;

    public void study(){
        System.out.println(this.name + "在学习...");
    }
}
package oop.demo02;

public class Application {
    public static void main(String[] args) {
        student xh = new student();
        student xm = new student();
        xh.name = "小红";
        xh.age = 13;
        xh.study();
        System.out.println(xh.name);
        System.out.println(xh.age);


    }
}

构造器详解

在这里插入图片描述
在这里插入图片描述
构造方法 快捷键 command + N
在这里插入图片描述

创建对象内存分析

在这里插入图片描述


封装详解

在这里插入图片描述

package oop.demo03;

public class student {
    private String name;
    private int id;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package oop.demo03;

public class Appaciaction {
    public static void main(String[] args) {
        student s1 = new student();
        s1.setName("qinf");
        System.out.println(s1.getName());

    }
}

继承

在这里插入图片描述

package oop.demo04;

public class Person {
    public String name = "winlo";
    private int age = 13;

    public void say(){
        System.out.println("讲了一句话...");
    }
}

package oop.demo04;

public class Student extends Person{

}
package oop.demo04;

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

super详解

package oop.demo04;

public class Person {
    protected String name = "person1";
}
package oop.demo04;

public class Student extends Person{
    public String name = "student1";

    public void test(String name){
        String nn = name;
        System.out.println(nn);

        System.out.println(this.name);
        System.out.println(super.name);
    }

}
package oop.demo04;

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.test("winl");
    }
}

在这里插入图片描述


package oop.demo04;

public class Person {

    protected void print(){
        System.out.println("Person");
    }
}
package oop.demo04;

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

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

}
package oop.demo04;

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

在这里插入图片描述


在new一个对象的时候会在调用自己的构造器的同时也会去调用父类的构造器;
显示调用父类构造器的话【super()】,要把它放在第一行;
调用this()也要放在第一行。

如果父类的无参构造没了,那么子类的无参构造也不能去生成了。

在这里插入图片描述

方法重写

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

什么是多态

在这里插入图片描述

package oop.demo06;

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

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

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

    }
}

instanceof和类型转换

在这里插入图片描述
有句话是:编译看左👈🏻,运行看右👉🏻

在这里插入图片描述

static 关键字详解

package oop.demo07;

public class Application {
    static {
        System.out.println("静态代码块");
    }

    {
        System.out.println("匿名代码块");
    }

    public Application() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Application application = new Application();
        System.out.println("============================");
        Application a1 = new Application();

    }
}

在这里插入图片描述

抽象类

在这里插入图片描述

继承抽象类的子类必须实现它的所有方法

在这里插入图片描述

接口

在这里插入图片描述

接口中所有定义的都是 public abstract
接口中定义的属性都是常量,也没人太会这么玩儿~

实现了接口的类就需要重写接口里的所有方法

接口可以多继承

package oop.demo09;

public interface UserSerice {
    void add(String id);
    void delete(String id);
}
package oop.demo09;

public interface TimeTiler {
    void timer();
}
package oop.demo09;

public class UserSericeImpl implements UserSerice,TimeTiler{
    @Override
    public void add(String id) {

    }

    @Override
    public void delete(String id) {

    }

    @Override
    public void timer() {

    }
}

在这里插入图片描述

内部类

在这里插入图片描述

package oop.demo10;

public class Outer {
    private int id = 10;
    public void out(){
        System.out.println("这是外部类");
    }
    public class inner {
        public void in(){
            System.out.println("这是内部类");
        }
        public void getID(){
            System.out.println(id);
        }
    }
}
package oop.demo10;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //outer.out();

        Outer.inner inner = outer.new inner();
        inner.in();
    }
}

内部类获取外部类的私有属性

一个Java类中可以有多个class类,但只能有一个public class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值