面向对象

一、类和对象

//类名 方法
//public为权限修饰器
public class people{
    //定义类的属性
    int height = 180;//成员变量
    int weight = 150;
    static quite = 30;//静态变量 用static声明,在方法之外
    //定义类的方法
    void cry(){
      System.out.println("CRY");
    }
    void attribute(){
    int height_local = 190 //局部变量(在类中但是在方法体之外的变量)
    }
}

1. 创建一个对象

类名 对象名 = new 类名();
People xiaohua = new People();
//创建后就可以是用类中的属性和方法了
System.out.println(xiaohua.height);
xiaohua.cry();
- 成员变量都可以用,局部变量只有方法内可以用,同时存在时局部变量优先。

2.构造方法

//每个类在创建时都会调用构造方法
//构造方法必须和类同名
public class PeopleAll{
    public static void main(String[] args){
    class People{
    int age;
    int height;
    //构造方法必须要有,同时要与类名相同
    public People(int a,int h){
        age = a
        height = h      
            }
        }
    }
}
//创建类时可以如果构造函数有参数要把参数加上
People num = new People(30, 180)

3.static 静态成员

public class PeopleAll{
    public static int age = 30; 
    public static void main(String[] args){
        //定义静态成员之后可以直接用类名进行调用
        //不用再创建一个对象单独使用
        System.out.println(PeopleAll.age)
        }
}

4.final关键字

//final修饰类,则类不可以被继承
//final修饰方法,则方法不允许被覆盖
//final修饰属性,则该属性不会在构造方法中被赋值
public class PeopleAll{
    public final static String shiyanlou = "实验楼";
    public static void main(String[] args){
        //输入 shiyanlou = "实验楼2" 会报错,因为shiyanlou作为已经被final修饰了。
        shiyanlou = "实验楼2";
        System.out.println(shiyanlou);
    }
}

二、封装

用private定义属性使其变为封装的属性只能用get和set方法进行获取和修改
public class Private{
    private int height = 180;
    //通过setter方法修改private数据
    public double setHeight(double newheight){
    height = newheight;
    }
    //通过getter方法返回private数据
    public double getHeight(){
       return height;
    } 
}
class People{
    Private people = new Private();
    public static void main(String[] args){
        System.out.println(people.getHeight);
        people.setHright(30);
        System.out.println(people.getHeight);
    }
}

三、继承

子类可以继承和复写父类的属性和方法

//可以使用extends继承父类
//子类更具体,父类更通用
public class Animals{
    public String bark = "动物叫";
    public void bark(){
       System.out.println("叫")
    }
}
class Dog extends Animals{
    public static void main(String[] args){
    Dog dudu = new Dog();
    System.out.println(dudu.bark);
    dudu.bark()
}
}

1.super关键字

//super关键字用于访问父类中的对象
//比如子类和父类中有一个相同名的方法或者变量,这个时候在子类想要使用父类中的变量就需要使用super方法
public class Animals{
    int age = 6;
}
class Dog extends Animals{
    int age = 8
    public static void main(String[] args){
    age = super.age;
    System.out.println(age)
    }
}

2.方法重载

方法重载一般用于创建一组任务相似但是参数不同的方法

3.方法重写

class Animals_test1{
    public void bark(){
        System.out.println("所有动物的叫");
    }
}
class Dogs extends Animals_test1{
    public void bark(){
        System.out.println("狗的叫");
    }
}

public class MyMain_test1{
    public static void main(String[] args){
        Animals_test1 animal = new Animals_test1();
        Dogs dog1 = new Dogs();
        animal.bark();
        dog1.bark();
    }
}

四、多态

1.向上转型

class Animals_test1{
    public void bark(){
        System.out.println("所有动物的叫");
    }
}
class Dogs extends Animals_test1{
    public void bark(){
        System.out.println("狗的叫");
    }
}

public class MAIN{
    Dogs d1 = new Dogs();
    //d2可以使用父类和子类的方法和属性,但不可以使用子类特有的方法和属性。
    Animals_test1 d2 = new Dogs();
}

2.抽象类

//抽象类中的抽象方法可以指定子类需要完成的方法;
abstract class Abstract{
    abstract void callMe();
    abstract void TellPhone();
}

class Abstract_son extends Abstract{
    public void callMe(){
        System.out.println("喊我");
    }

    public void TellPhone(){
        System.out.println("电话打给我");
    }
}

public class MyMain_test2 {
    public static void main(String[] args){
        Abstract_son son1 = new Abstract_son();
        son1.callMe();
        son1.TellPhone();
    }
}

3.接口


//JK接口

interface Animal{
    int y = 30;
    public void eat();
    public void travel();
}


//接口的实现
public class JK implements Animal{
    public void eat(){
        System.out.println("eat");
    }

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

    public static void main(String[] args){
        JK jk_eat = new JK();
        JK jk_travel = new JK();
        jk_eat.eat();
        jk_travel.travel();
    }
}

4.成员内部类

public class People {
    private String name = "all_people";

    public class Student {
        private String ID = "2121190009";

        public void callMe() {
            System.out.println("内部类可以直接访问外部类的属性(包括private属性)如姓名" + name);
            System.out.println("I am in people.student");
        }
    }
        public static void main(String[] args){
            People people1 = new People();
            //相要创建内部类对象必须先创建外部类对象 用外部类对象访问内部类对象
            Student student1 = people1.new Student();
        }
}

5.静态内部类

public class People {
    private String name = "all_people";

    static String ID = "193839292992838XX";

    //定义静态内部类
    public static class Student {
        String ID = "2121190009";

        public void stuInfo() {
            //打印内部类的ID
            System.out.println("内部的ID为 " + ID);
            //打印外部类的静态ID
            System.out.println("外部类的静态ID为 "+People.ID);
            //打印外部类的name
            //静态类不能直接访问外部的非静成员 必须用new 外部类姓名().非静态成员
            System.out.println("外部类的name为 "+new People().name);
        }
    }
        public static void main(String[] args) {
            /*静态内部类对象创建可以直接用内部类名字创建
            不用像成员内部类对象一样先创建外部类对象
            再用外部类对象创建内部类对象
             */
            Student in = new Student();
            in.stuInfo();
        }
    }

6.局部类

public class InClass {
    public void peopleInfo(){
        final String sex = "man";
        //创建一个局部类(定义在内部类的方法和作用域内)
        class Student{
            String ID = "20151234"; //内部类作用域中
            public void print() {
                System.out.println("外部类的常量: " + sex);
                System.out.println("内部类中的变量: " + ID);
            }

        }
        Student student1 = new Student();
        student1.print();
    }
    public void peopleInfo2(boolean b){
        if(b){
            final String sex ="man";
            class Student{
                String ID = "20151234";
                public void print2(){
                    System.out.println("外部类的常量: " + sex);
                    System.out.println("内部类的常量: " + ID );
                }
            }
            Student student1 = new Student();
            student1.print2();
        }
    }
    public static void main(String[] args){
        InClass b_noIf = new InClass();
        b_noIf.peopleInfo();
        InClass b_if = new InClass();
        b_if.peopleInfo2(true);
        b_if.peopleInfo2(false);
    }
}

本文章使用limfx的vscode插件快速发布