四、java面向对象基础

1、认识类和对象

通俗的讲,类是设计图,对象是类的实例

public class Test {

    public static void main(String[] args) {
        //创建对象:类   变量名=new 类();
        //new 类();叫创建对象,作用是分配空间
        Computer c = new Computer();
        
        //为对象的属性赋值:对象.属性名
        c.color = "白色";
        c.size = 15.6;
        c.memory = 8;
        c.cpu = "i7";
        
        //调用对象的方法:对象.方法名()
        c.start();
        c.close();
        c.display();
        
        //注意创建多个对象,彼此各自拥有一套类的属性
        Computer c2 = new Computer();
        c2.color = "red";
        c2.size = 10.5;
        c2.display();

    }

2、类的属性、方法、构造器

(1)属性

属性的类型:8种基本数据类型,引用数据类型

public class Girl {
    String name;//属性类型 属性名
    int age;
    String face;

}
public class Boy {
    String name;
    int age;
    double money;
    Girl girlFriend;//Girl是引用数据类型
    
    
    public static void main(String[] args) {
        Girl girl = new Girl();
        girl.name = "小芳";
        girl.age = 18;
        girl.face = "好看";
        
        Girl girl2 = new Girl();
        girl2.name = "小红";
        girl2.age = 18;
        girl2.face = "好看";
        
        Boy boy = new Boy();
        boy.name = "蔡徐坤";
        boy.age = 19;
        boy.money = 100000000;            
    }
}

变量的位置:变量(成员变量)=属性=field

public class Girl {
    String name;//成员变量:在方法体外,类体内
    int age;
    String face;
    public void test() {
        int t;//局部变量:在方法体内的变量
    }
}

属性的默认值:与数组相同

(2)方法=函数=method(方法不能独立存在),方法只有被调用才会被执行

无返回值

public class Calculator {
    public void add() {//无返回值:在方法名前面使用void
        int x = 1;
        int y = 3;
        int result = x + y;
        System.out.println(result);    
    }
    public static void main(String[] args) {
        Calculator c = new Calculator();
        c.add();
    }
}

有返回值

public class Calculator2 {
    public int add() {//有返回值:方法名前面使用类型(8种基本数据类型或者引用数据类型)
        int x = 1;
        int y = 3;
        int result = x + y;
        return result;//将结果返回给调用者
        //注意:以下三者要一致:return后面的数据类型、方法前面的数据类型、调用地方的变量类型
    }
    public static void main(String[] args) {
        Calculator2 c = new Calculator2();
        int r = c.add();//获取方法的返回值
        r = r + 3;
        System.out.println(r);
        c.add();//可以获取方法的返回值,但是不太好        
    }
}

方法的参数

public class Calculator3 {
    public int add(int x,int y) {
        int result = x + y;
        return result;
    }    
    public int jian(int x,int y) {
        int result = x - y;
        return result;
    }
    public int cheng(int x,int y) {
        int result = x * y;
        return result;
    }
    public double chu(double x,double y) {
        double result = x / y;
        return result;
    }
    public static void main(String[] args) {
        Calculator3 c = new Calculator3();
        int r = c.add(1,3);
        System.out.println(r);
        
        r = c.jian(3, 6);
        System.out.println(r);
        
        r = c.cheng(7, 9);
        System.out.println(r);
        
        double d = c.chu(15, 6);
        System.out.println(d);        
    }
}

方法的重载要求:方法在同一个类中;方法名必须相同;方法的参数列表不同(参数的个数不同、参数的类型不同)

(3)构造器

public class Employee {
    String name;
    int age;
    String type;
    //构造器=构造函数=构造方法=constructor
    //如果在类中没有显示定义构造器,程序会默认提供空参构造器
    public Employee() {
        System.out.println("hello");
    }
    //构造器的作用:创建对象(在内存中申请一块空间)、为对象的属性赋值
    public Employee(String n,int a,String t) {
        System.out.println("world");
        name = n;
        age = a;
        type = t;
    }
    public String toString() {
        return "Employee [name=" + name + ", age=" + age + ", type=" + type + "]";
    }
    //如果我们自己显示定义构造器,默认的构造器不再提供
public static void main(String[] args) { Employee e = new Employee(); e.name = "张三"; e.age = 25; e.type = "职员"; Employee e2 = new Employee(); e2.name = "李四"; e2.age = 29; e2.type = "经理"; Employee e3 = new Employee("jack",19,"职员"); Employee e4 = new Employee("tom",19,"职员"); System.out.println(e3.toString()); System.out.println(e4.toString());

3、面向对象三大特征:继承、封装、多态

(1)继承:子类继承父类,子类可以共用父类中的属性和方法,提高了代码的复用性,子类无法继承父类的构造函数,继承是单继承

public class Animal {//父类
    String name;
    public void display() {
        System.out.println("姓名:" + name);
    }
}
public class Cat extends Animal{//子类
}
public class Dog extends Animal{//子类
}
public class TestAnimal {//测试类
    public static void main(String[] args) {
        Cat c = new Cat();
        c.name = "小猫";
        c.display();
        
        Dog d = new Dog();
        d.name = "小猫";
        d.display();        
    }
}

(2)封装:封装内部数据,属性使用private修饰,外部访问时通过增加get和set方法

public class Tester {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        //增加验证逻辑
        if(age <= 150 && age >= 0) {
            this.age = age;
        } else {
            System.out.println("年龄设置失败");
        }
    }
    public int getAge() {
        return age;
    }
    public Tester(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public class TestTester {
    public static void main(String[] args) {
        Tester t = new Tester("jack", 20);
        t.setAge(200);
        t.setAge(-30);
        int a = t.getAge();
        System.out.println(a);
    }
}

(3)多态

public class Fruit {
    String name;
}
public class Orange extends Fruit {
}
public class Banana extends Fruit{
}
public class Test {
    public void test(String str) {//传string类型的参数
        
    }
    public void eat(Orange o) {
        System.out.println("吃:" + o.name);    
    }
    public void eat(Banana o) {
        System.out.println("吃:" + o.name);    
    }
    public void eat2(Fruit o) {
        System.out.println("吃:" + o.name);    
    }
    public static void main(String[] args) {
        //之前创建对象方式
        Orange o = new Orange();
        o.name = "橙子";
        
        Banana b = new Banana();
        b.name = "香蕉";
        
        //多态:创建对象:父类的声明,子类的实例化
        Fruit f1 = new Orange();
        f1.name = "橙子2";
        
        Fruit f2 = new Banana();
        f2.name = "香蕉2";
        
        Test t = new Test();
        t.eat(o);
        t.eat(b);
        //多态的好处,只需要写一个eat2方法即可
        t.eat2(o);
        t.eat2(b);
        t.eat2(f1);
        t.eat2(f2);
    }
}

4、关键字

(1)static

//static修饰变量-类变量
public
class Circle { double r;//实例变量 static double PI = 3.14;//类变量 public Circle(double r) { this.r = r; } public static void main(String[] args) { Circle c = new Circle(3); Circle c2 = new Circle(5); System.out.println(c.r + "," + c.PI); System.out.println(c2.r + "," + c2.PI); c.r = 1; //c.PI = 5.14;无法修改final修饰的变量 System.out.println(c.r + "," + c.PI); System.out.println(c2.r + "," + c2.PI); //类变量被所有对象共用 //访问类变量方式:对象.类变量,类名.类变量(最常用) System.out.println(Circle.PI); } }
//static修饰方法
public class ArrayUtil {
    public static void getMax(int[] arr) {
        int max = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println(max);

    }
    public static void main(String[] args) {
        //方法调用方式:对象.方法名()(需要创建对象);类型.方法名()(不需要创建对象,使用较多)
        int[] arr = new int[] {13,15,67,99,100};
        ArrayUtil.getMax(arr);
        int[] arr2 = new int[] {77,15,69,99,110};
        ArrayUtil.getMax(arr2);
    }
}

(2)final

final修饰的类不能被继承

final修饰的属性不能被修改

final修饰方法时,子类无法重写此方法(保护方法的稳定性)

(3)abstract

abstract class Plant {//抽象类,有些类不需要实例化    
    public void test() {//抽象类中包含普通的方法        
    }
    //抽象类中可以包含抽象方法;抽象方法必须被子类重写;如果一个类中有抽象方法,那么此类必须是抽象类
    public abstract void jiaoshui();
}

class Tree extends Plant{
    public void jiaoshui() {
        System.out.println("给树浇水");
    }
}
class Grass extends Plant{
    public void jiaoshui() {
        System.out.println("给草浇水");
    }
} 
abstract class Flower extends Plant{//如果子类不重写父类中的抽象方法,那么子类必须是抽象类
    
}
public class Test{
    public static void main(String[] args) {
        Tree t = new Tree();
        Grass g = new Grass();
        
        Plant p1 = new Tree();
        p1.jiaoshui();
        Plant g1 = new Grass();
        g1.jiaoshui();        
    }    
}

(4)interface(定义接口)和implements(实现接口)

public class Father {
}
public interface Culture {
    public void speakEnglish();
    public void writeArtical();
}
public interface Sport {
    public void swim();
    public void playFootball();
}
public class Uncle implements Sport {
    public void swim() {
        System.out.println("叔叔游泳");
    };
    public void playFootball() {;
        System.out.println("叔叔踢足球");
    }
}
public class Son extends Father implements Sport,Culture{
    public void swim() {
        System.out.println("儿子游泳");
    };
    public void playFootball() {
        System.out.println("儿子踢足球");
    };
    public void speakEnglish() {;
        System.out.println("儿子说英语");
    }
    public void writeArtical() {;
        System.out.println("儿子写文章");
    }
}
public class Test {
    public static void main(String[] args) {
        Son s = new Son();
        s.swim();
        s.playFootball();
        s.speakEnglish();
        s.writeArtical();
        
        Uncle u = new Uncle();
        u.swim();
        u.playFootball();
        
        Father f = new Son();

        Sport s1 = new Son();
        s1.swim();
        s1.playFootball();
        
        Sport s2 = new Uncle();
        s2.swim();
        s2.playFootball();
        
        Culture c = new Son();
        c.speakEnglish();
        c.writeArtical();        
    }
}

猜你喜欢

转载自www.cnblogs.com/qianyyue/p/10896826.html