【java】面向对象的编程基础

文章目录

  • 面向对象的编程基础
    • 定义
    • 方法重载
    • 执行顺序
    • 静态变量和方法
    • 加载顺序
    • 包和访问控制
    • 类的封装
    • object类
    • 方法重写
    • 抽象类
    • 接口
    • 枚举类

面向对象的编程基础

定义

public class person  {
String name;
int age;
char sex;
person(String name,int age,char sex)
{
    this.age=age;
    this.name=name;
    this.sex=sex;
}
person()
{}
}
public class Main {
    public static void main(String[] args) {
        person p1 = new person();   //这两个变量分别引用的是不同的两个对象
        person p2 = new person();
        person p3=p1;
        System.out.println(p1 == p3);
        System.out.println(p1 == p2);   //如果两个变量存放的是不同对象的引用,那么肯定就是不一样的了
    }
}

true
false

public class Main {
    public static void main(String[] args) {
        person p1 = new person();   //这两个变量分别引用的是不同的两个对象
        p1.setName("coleak");
        System.out.println(p1.getName());
    }
}
public class person  {
String name;
int age;
char sex;
person(String name,int age,char sex)
{
    this.age=age;
    this.name=name;
    this.sex=sex;
}
person()
{}
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

这里输入set和get可以自动将函数补全,传参初始化在构造属性之后

方法重载

int sum(int a, int b){
    return a + b;
}

double sum(double a, double b){    //为了支持小数加法,我们可以进行一次重载
    return a + b;
}

执行顺序

public class Main {
    public static void main(String[] args) {
        person p1 = new person("coleak",19,'n');
        System.out.println(p1.name);
    }
}
public class person  {
String name="cc";
int age=1;
char sex='n';
person(String name,int age,char sex)
{
    System.out.println(this.name);
    this.age=age;
    this.name=name;
    this.sex=sex;
}
{
    System.out.println(this.name);
    this.name="ccccc";
}
}

cc
ccccc
coleak

这里说明先构造属性,再执行代码块,再初始化

静态变量和方法

public class Main {
    public static void main(String[] args) {
        person p1 = new person("coleak",19,'n');
        for(int i=0;i<3;i++) {
            person p = new person();
        }
        System.out.println(person.num);
        person.test();
    }
}
public class person  {
String name="cc";
int age=1;
char sex='n';
static int num=0;
person()
{
    num++;
}
person(String name,int age,char sex)
{
    this.age=age;
    this.name=name;
    this.sex=sex;
    num++;
}
static void test()
{
    System.out.println(num);
    //System.out.println(age);不能调用非static的成员变量和方法
}
static {
    System.out.println("静态代码块");
}
}

静态代码块
4
4

加载顺序

public class person {
        String name = test();  
    	int age;
        String sex;

        {
            System.out.println("我是普通代码块");
        }

        person(){
            System.out.println("我是构造方法");
        }

        String test(){
            System.out.println("我是成员变量初始化");
            return "小明";
        }

        static String info = init();

        static {
            System.out.println("我是静态代码块");
        }

        static String init(){
            System.out.println("我是静态变量初始化");
            return "test";
        }
    }
public class Main {
    public static void main(String[] args) {
        person p=new person();
    }
}

我是静态变量初始化
我是静态代码块
我是成员变量初始化
我是普通代码块
我是构造方法

包和访问控制

package com.test;
import com.test.entity.*;

public class Main {
    public static void main(String[] args) {
        person p=new person();
        p.name="coleak";
        java.lang.System.out.println(p.name);
    }
}
package com.test.entity;
public class person {
        public String name;  //这里我们用test方法的返回值作为变量的初始值,便于观察
        int age;
        String sex;
        public person(){
            System.out.println("我是构造方法");
        }
    }

【java】面向对象的编程基础_第1张图片

类的封装

快速构造,点击生成,构造函数,全选即可。

package com.test;
import com.test.entity.*;

public class Main {
    public static void main(String[] args) {
        person p=new person("coleak",18,"nan");
        java.lang.System.out.println(p.getName());
        p.setName("xax");
        System.out.println(p.getName());
        p.setName("cc");
        System.out.println(p.getName());
    }
}
package com.test.entity;
public class person {
        private String name;  //这里我们用test方法的返回值作为变量的初始值,便于观察
        private int age;
        private String sex;
        public person()
        {
            System.out.println("我是构造方法");
        }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

    public void setName(String name) {
        if (name.contains("a"))  return;
        this.name = name;
    }

    public String getSex() {
        return sex;
    }
    public person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

coleak
coleak
cc

类的继承

package com.test.entity;

public class student extends worker{
    public student(String name, int age, String sex) {
        super(name, age, sex);
    }

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


package com.test.entity;
public class person {
         protected String name;  //这里我们用test方法的返回值作为变量的初始值,便于观察
         int age;
         String sex;
       public void per()
         {
             System.out.println("person");
         }
    person(){}
    public person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

package com.test.entity;

public class worker extends person {
    public worker(String name, int age, String sex) {
        super(name, age, sex);
    }
    public void wor()
 {
     System.out.println("worker");
 }
    public worker(){}
}


package com.test;
import com.test.entity.*;
public class Main {
    public static void main(String[] args) {
        student s1=new student("coleak",19,"nan");
        s1.wor();
        s1.stu();
        s1.per();
        person w1=new worker();//以父之名,只能访问父类的内容
        w1.per();
        worker w2=(worker) w1;//强制类型转换,原本就是worker的情况
        w2.wor();
        w2.per();
        if(w1 instanceof worker&&w1 instanceof person)//对应类型或者是其继承类
        {
            System.out.println("YES");
        }
    }
}

worker
stu
person
person
worker
person
YES

与父类同名属性,就近原则使用本身的属性值。

访问父类时加上super.name

object类

package com.test;
import com.test.entity.*;
public class Main {
    public static void main(String[] args) {
        student s1=new student("coleak",19,"nan");
        student s2=s1;
        student s3=new student("coleak",19,"nan");
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1);//默认调用tostring
    }
}

true
false
com.test.entity.student@3ac3fd8b

方法重写

final 无法重写,无法重新赋值,无法继承

子类无法低于父类的可见性

package com.test.entity;
public class person {
    protected String name;  //这里我们用test方法的返回值作为变量的初始值,便于观察
    int age;
    String sex;

    person() {
    }

    ;

    public person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof person) {     //只有是当前类型的对象,才能进行比较,要是都不是这个类型还比什么
            person person = (person) obj;   //先转换为当前类型,接着我们对三个属性挨个进行比较
            return this.name.equals(person.name) &&    //字符串内容的比较,不能使用==,必须使用equals方法
                    this.age == person.age &&       //基本类型的比较跟之前一样,直接==
                    this.sex.equals(person.sex);
        }
        return false;
    }

    @Override
    public String toString() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

package com.test;
import com.test.entity.*;
public class Main {
    public static void main(String[] args) {
        student s1=new student("coleak",19,"nan");
        student s2=s1;
        student s3=new student("coleak",19,"nan");
        System.out.println(s1.equals(s3));//调用重写后的函数,实际对象是student
        System.out.println(s1);
    }
}

true
person{name=‘coleak’, age=19, sex=‘nan’}

抽象类

子类如果不是抽象类则不实现父类的抽象方法会报错

无法直接调用抽象类的实例方法去构造对象

package com.test.entity;
public abstract class person {
    protected String name;  //这里我们用test方法的返回值作为变量的初始值,便于观察
    int age;
    String sex;
    public person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    protected abstract void eaxm();
}

package com.test.entity;

public class student extends worker{
    public student(String name, int age, String sex) {
        super(name, age, sex);
    }

    @Override
    public void learn() {
        System.out.println("学生学习");
    }
    public void stu(){
        System.out.println("stu");
    }
}

package com.test.entity;

public abstract class worker extends person {
    public worker(String name, int age, String sex) {
        super(name, age, sex);
    }

    @Override
    protected void eaxm() {

    }
    protected abstract void learn();
}

package com.test;
import com.test.entity.*;
public class Main {
    public static void main(String[] args) {
        student s1=new student("coleak",19,"nan");
        s1.learn();
    }
}

接口

继承只能单继承,但是可以连接多个接口

接口可以继承,且继承父类的全部方法,因此子接口被引用时,类需要实现其父接口的所有方法

package com.test;
import com.test.entity.*;
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Study s1=new student("coleak",19,"nan");
        s1.study();
        student s2=(student) s1;
        s2.test();
        s2.test2();
        System.out.println(Study.a);
        System.out.println(s1.a);
        System.out.println(s2.a);
        System.out.println(student.a);
        //s1.test2();报错,只能使用接口内定义的方法
        student clone= (student) s2.clone();
        System.out.println(clone==s2);
    }
}

package com.test.entity;

public class student extends worker implements Study,Cloneable{
    public student(String name, int age, String sex) {
        super(name, age, sex);
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public void study() {
        System.out.println("stu");
    }
    public void test2()
    {
        System.out.println("test2");
    }
}

package com.test.entity;
public interface Study {
    void study();
    //设置默认实现,子类可以不重写
    default void test()
    {
        System.out.println("默认实现");
    }
     public static final int a=10;//默认就是这个
}

stu
默认实现
test2
10
10
10
10
false

枚举类

package com.test.entity;

public class student {
    private Status status;

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}

package com.test.entity;
public enum Status {
        RUNNING("睡觉"), STUDY("学习"), SLEEP("睡觉");   //无参构造方法被覆盖,创建枚举需要添加参数(本质就是调用的构造方法)

        private final String name;    //枚举的成员变量
        Status(String name){    //覆盖原有构造方法(默认private,只能内部使用!)
                this.name = name;
        }

        public String getName() {   //获取封装的成员变量
                return name;
        }
}

package com.test.entity;
public class Main {
    public static void main(String[] args) {
    student s1=new student();
    s1.setStatus(Status.SLEEP);
        System.out.println(s1.getStatus().name());
        System.out.println(s1.getStatus().getName());
//        System.out.println(s1.);
    }
}

SLEEP
睡觉

你可能感兴趣的:(java,java,jvm,开发语言)