Java 面向对象

简单的面向对象编程

导包:指出需要使用的类,在什么位置?

创建:类名称  对象名  =  new  类名()

使用:成员变量, 成员方法

public class Phone {
//    成员变量
    String brand;
    double price;
    String color;

//    成员方法
    public void call(String who){
        System.out.println("call" + who);
    }
    public void sendMessage(){
        System.out.println("send message to Anson");
    }
    
//     构造方法
    public static void main(String[] args) {
        Phone iphone = new Phone();
        iphone.brand = "apple";
        iphone.price = 6999;
        iphone.color = "black";
        System.out.println("brand:" + iphone.brand);  // brand:apple
        System.out.println("price:" + iphone.price);  // price:6999.0
        System.out.println("color:" + iphone.color);  // color:black
    }
}

使用对象类型作为方法的参数:

当一个对象作为参数,传递到方法当中时,实际上传递进去的是对象的地址值(引用)

public class DemoPhoneParam{
    public static void main(String[] args){
        // 使用上面的Phone类
        Phone one = new Phone();
        one.brand = "apple";
        one.price = 5998.0;
        one.color = "black";

        // 调用方法,传递对象参数
        method(one);
    }

    public static void method(Phone one){
        System.out.println(one.brand);
        System.out.println(one.price);
        System.out.println(one.color);
    }
}

使用对象类型作为方法的返回值:

当方法的返回值是一个对象时,实际传递的也是该对象的地址值(引用)

public class DemoPhoneReturn{
    public static void main(String[] args){
        // 调用方法,接收对象返回值
        Phone one = getPhone();
        System.out.println(one.brand);
        System.out.println(one.price);
        System.out.println(one.color);
    }

    public static Phone getPhone(){
        // 仍然使用上面的Phone类
        Phone one = new Phone();
        one.brand = "apple";
        one.price = 5998.0;
        one.color = "black";
        return one;
    }
}

成员变量和局部变量的区别

  1. 定义的位置不一样
    • 成员变量定义在方法外部             
    • 局部变量定义在方法内部
  2. 作用域不一样,  
    • 成员变量在整个类中都可以使用
    • 局部变量只有在方法中可以使用,出了方法不能使用
  3. 默认值不一样,     
    • 成员变量有默认值,规则和数组一样
    • 局部变量没有默认值,使用前必须赋值
  4. 内存的位置不一样,
    • 成员变量在堆内存中
    • 局部变量在栈内存中
  5. 生命周期不一样,   
    • 成员变量随对象创建而产生,随对象被垃圾回收而消失
    • 局部变量随方法进栈而产生,随方法执行完毕(出栈)而消失

    

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

封装在java当中的体现

方法就是一种封装:

public class DemoMethod{
    public static void main(String[] args){
        int[] array = {1, 2, 3, 4, 5};
        int result = getMax(array);
        System.out.println(result);
    }
    
    // 封装一个取最大值的方法
    public static int getMax(int[] array){
        int max = array[0];
        for (int i = 1; i < array.length; i++){
            if (array[i] > max){
                max = array[i];
            }
        }
        return max;
    }
}

关键字 private 也是一种封装,this 关键字:谁调用方法,this 就是该对象的地址值

// 对于基本数据类型当中的boolean值,Getter方法一定要写成isXxx的形式,而setXxx规则不变

public class Student{
    private String name;
    private int age;
    private boolean male;

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }

    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }

    public void setMale(boolean male){
        this.male = male;
    }
    public boolean isMale(){
        return male;
    }

    public static void main(String[] args){
        Student student = new Student();
        student.setName("johny");
        student.setAge(18);
        student.setMale(false);
        System.out.println(student.getName());  // johny
        System.out.println(student.getAge());  // 18
        System.out.println(student.isMale());  // false
    }
}

构造方法

构造方法是专门用来创建对象的方法,当我们通过关键字 new 来创建对象时,其实就是在调用构造方法

注意事项:

  1. 构造方法的名称必须和类名保持一致,包括大小写
  2. 构造方法没有返回值,连 void 也不用写
  3. 如果没有手动编写构造方法,那么会使用编译器中的默认构造方法,没有参数,方法体什么都不做,只是创建对象
  4. 一旦编写了构造方法,那么就不再使用默认的构造方法
  5. 构造方法也是可以重载的(方法名相同,参数列表不同)
public class Constructor {
    private String name;
    private int age;
    
    public Constructor(){
        System.out.println("无参的构造方法");
    }
    
    public Constructor(String name, int age){
        System.out.println("全参的构造方法执行");
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        Constructor constructor_args = new Constructor("Anson", 12);
        Constructor constructor = new Constructor();
    }
}

定义一个标准的 java bean

一个标准的 java bean 通常要拥有下面4个组成部分

  1. 所有的成员变量都要使用 private 关键字修饰
  2. 为每一个成员变量编写一对 Getter /  Setter 方法
  3. 编写一个无参数的构造方法
  4. 编写一个全参数的构造方法
public class Person {
    private String name;
    private int age;

    // 无参构造
    public Person(){
    }

//    全参构造
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }

    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return this.age;
    }

    public static void main(String[] args) {
        Person person = new Person();
        Person personArgs = new Person("Anson", 12);
        System.out.println(personArgs.name + ", " + personArgs.age);

        person.setName("randomName");
        person.setAge(99);
        System.out.println(person.getName() + ", " + person.getAge());
    }
}

面向对象继承,多态,转到下一篇

ending ~

每天都要遇到更好的自己.
原文地址:https://www.cnblogs.com/kaichenkai/p/10795814.html