面向对象

1.手机类的实现

package Phone;

public class Demo_phone {
    public static void main(String[] args){
        Phone phone=new Phone();
        phone.brand="huawei mate30";
        phone.color="green";
        phone.price=4999.0;
        System.out.println(phone.brand+"	"+phone.price+"	"+phone.color);

        phone.call("Jack");
        phone.watch();
        phone.play();

    }
}

 完整版

package Phone;

public class Demo_phone {
    public static void main(String[] args){
        Phone phone=new Phone();
        phone.brand="huawei mate30";
        phone.color="green";
        phone.price=4999.0;
        System.out.println(phone.brand+"	"+phone.price+"	"+phone.color);

        phone.call("Jack");
        phone.watch();
        phone.play();

    }

    public static class Phone {
        String brand;
        double price;
        String color;
        public void call(String who){
            System.out.println("call "+who);
        }
        public void play(){
            System.out.println("play");
        }
        public void watch(){
            System.out.println("watch");
        }
    }
}

 2.一个对象的内存图

3.两个对象使用同一方法的内存图

 

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

code:

package Phone;

public class Demo_phone2 {
    public static void main(String[] args){
        Phone phone2=new Phone();
        phone2.brand="apple";
        phone2.color="black";
        phone2.price=5999;
        method(phone2);
    }
    public static void method(Phone param){
        System.out.println(param.brand);
        System.out.println(param.color);
        System.out.println(param.price);

    }
    public static class Phone {
        String brand;
        double price;
        String color;
        public void call(String who){
            System.out.println("call "+who);
        }
        public void play(){
            System.out.println("play");
        }
        public void watch(){
            System.out.println("watch");
        }
    }
    /*
    ret:apple
        black
        5999.0
     */
}

 5.将对象类型作为方法的返回值

code:

package Phone;

public class Demo_phone3 {
    public static void main(String [] args){
        Phone phone3=getphone();
        System.out.println(phone3);
        System.out.println(phone3.brand);
        /*
        ret:Phone.Demo_phone3$Phone@71bc1ae4
            Phone.Demo_phone3$Phone@71bc1ae4
            huawei
         */
    }
    public static Phone getphone(){
        Phone three=new Phone();
        three.brand="huawei";
        three.color="green";
        three.price=3599;
        System.out.println(three);
        return three;
    }

    public static class Phone {
        String brand;
        double price;
        String color;
        public void call(String who){
            System.out.println("call "+who);
        }
        public void play(){
            System.out.println("play");
        }
        public void watch(){
            System.out.println("watch");
        }
    }
}

 6.一个完整的类

package student;

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

    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    private String name;
    private int age;
    private String sex;
    /*
    public Student(){
        System.out.println("hello student!");
    }
    public Student(String name,int age,String sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
    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 setSex(String sex){
        this.sex=sex;
    }
    public String getSex(){
        return sex;//ret:hello student
                    hull
                    male
                    18 
    }
*/


}

测试代码:

package student;

public class Demo_student {
    public static void main(String [] args){
        Student stu1=new Student();
        stu1.setAge(18);
        stu1.setName("Hull");
        stu1.setSex("male");
        System.out.println(stu1.getSex()+"	"+stu1.getName()+"	"+stu1.getAge());
    }
}
原文地址:https://www.cnblogs.com/resort-033/p/12965750.html