1、定义手机类,手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能。

2、定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。

1. 手机类

请定义出手机类,类中要有空参、有参构造方法,set/get方法。

定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。

调用对象的两个功能,打印效果如下:

正在使用价格为3998元黑色的小米手机打电话…. 正在使用价格为3998元黑色的小米手机发短信….

在idea编辑器中,定义属性后,可以通过右键或者Alt + Insert快捷键快速生成构造方法和get/set方法

手机类:Phone

public class Phone {
    private String brand;
    private double price;
    private String color;

    //定义无参构造方法
    public Phone() {
        System.out.println("调用无参构造方法");
    }

    //定义有参构造方法
    public Phone(String brand, double price, String color) {
        System.out.println("调用有参构造方法");
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    //打电话行为
    public void call() {
        System.out.println("正在使用价格为:" + this.price + "元" + this.color + "的" + this.brand + "手机打电话...");
    }

    //发短信行为
    public void sendMessage() {
        System.out.println("正在使用价格为:" + this.price + "元" + this.color + "的" + this.brand + "手机发短信...");
    }

}

测试类:TestPhone

public class TestPhone {
    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.setBrand("小米");
        phone.setPrice(3998);
        phone.setColor("黑色");

        phone.call();
        phone.sendMessage();
    }
}

程序输出:

调用无参构造方法
正在使用价格为:3998.0元黑色的小米手机打电话...
正在使用价格为:3998.0元黑色的小米手机发短信...

2. 女朋友类。

我女朋友叫凤姐,身高155.0厘米,体重130.0斤 女朋友帮我洗衣服 女朋友给我做饭

女朋友类:GirlFriend

public class Girlfriend {
    private String name;
    private double height;
    private double weight;

    public String getName() {
        return name;
    }

    public Girlfriend(String name, double height, double weight) {
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void show() {
        System.out.println("我是" + getName() + "。我的身高是:" + getWeight() + "厘米, " + "我的体重是" + getWeight() + "斤...");
    }

    public void wash() {
        System.out.println("我会洗衣服");
    }
    public void cook() {
        System.out.println("我还会做饭");
    }
}

测试类:TestGirlfriend

public class TestGirlFriend {
    public static void main(String[] args) {
        Girlfriend girlfriend = new Girlfriend("凤姐", 155.0, 130);
        girlfriend.show();
        girlfriend.wash();
        girlfriend.cook();
    }
}

程序输出:

我是凤姐。我的身高是:130.0厘米, 我的体重是130.0斤...
我会洗衣服
我还会做饭