学习视频链接:https://www.bilibili.com/video/BV17F411T7Ao
定义手机类,手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能。
请定义出手机类,类中要有空参、有参构造方法,set/get方法。
定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。
调用对象的两个功能,打印效果如下:
正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
package cn.kox.homework.one;
/**
* @ClassName: Phone
* @Author: Kox
* @Data: 2023/1/19
* @Sketch: 题目1
*/
public class Phone {
private String brand;
private double price;
private String color;
// 空参构造
public Phone() {
}
// 带参构造
public Phone(String brand, double price, String color) {
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;
}
private void use() {
System.out.print("正在使用价格为" + price + "元" + color + "的" + brand + "手机");
}
public void call() {
use();
System.out.println("打电话...");
}
public void set() {
use();
System.out.println("发短信...");
}
}
package cn.kox.homework.one;
/**
* @ClassName: phoneDemo
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class phoneDemo {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.setBrand("小米");
p1.setPrice(4999);
p1.setColor("白色");
p1.call();
Phone p2 = new Phone("苹果", 5399, "蓝色");
p2.set();
}
}
定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
我女朋友叫凤姐,身高155.0厘米,体重130.0斤
女朋友帮我洗衣服
女朋友给我做饭
package cn.kox.homework.two;
/**
* @ClassName: Girlfriend
* @Author: Kox
* @Data: 2023/1/19
* @Sketch: 题目2
*/
public class Girlfriend {
private String name;
private double height;
private double weight;
public Girlfriend() {}
public Girlfriend(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
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("我女朋友叫" + name +",身高" + height + "厘米,体重" + weight + "斤");
}
public void wash() {
System.out.println("女朋友喊我洗衣服");
}
public void cook() {
System.out.println("女朋友喊我做饭");
}
}
package cn.kox.homework.two;
/**
* @ClassName: girlfriendDemo
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class girlfriendDemo {
public static void main(String[] args) {
Girlfriend g1 = new Girlfriend("苏菲", 170, 105);
g1.show();
g1.cook();
g1.wash();
}
}
定义项目经理类Manager。属性:姓名name,工号id,工资salary,奖金bonus。行为:工作work()
定义程序员类Coder。属性:姓名name,工号id,工资salary。行为:工作work()
要求:
1.按照以上要求定义Manager类和Coder类,属性要私有,生成空参、有参构造,set和get方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法)
3.调用成员方法,打印格式如下:
工号为123基本工资为15000奖金为6000的项目经理张三正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....
工号为135基本工资为10000的程序员李四正在努力的写着代码......
package cn.kox.homework.three;
/**
* @ClassName: Manager
* @Author: Kox
* @Data: 2023/1/19
* @Sketch: 题目3
*/
public class Manager {
private String name;
private int id;
private int salary;
private int bonus;
public Manager() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public Manager(String name, int id, int salary, int bonus) {
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public void work() {
System.out.println("工号为" + id +
"基本工资为" + salary +
"奖金为" + bonus +
"项目经理" + name +
"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
}
package cn.kox.homework.three;
/**
* @ClassName: Coder
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class Coder {
private String name;
private int id;
private int salary;
public Coder(String name, int id, int salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public void work() {
System.out.println("工号为" + id +
"基本工资为" + salary +
"的程序员" + name +
"正在努力的写着代码......");
}
}
package cn.kox.homework.three;
/**
* @ClassName: managerDemo
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class Demo {
public static void main(String[] args) {
Manager m1 = new Manager();
m1.setName("Kox");
m1.setId(521);
m1.setSalary(20000);
m1.setBonus(8000);
m1.work();
Coder c1 = new Coder("科克斯", 123, 15000);
c1.work();
}
}
定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse()
定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家lookHome()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,set和get方法
2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法)
3.调用成员方法,打印格式如下:
花色的波斯猫正在吃鱼.....
花色的波斯猫正在逮老鼠....
黑色的藏獒正在啃骨头.....
黑色的藏獒正在看家.....
package cn.kox.homework.four;
/**
* @ClassName: Cat
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class Cat {
private String color;
private String breed;
public Cat() {}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void eat() {
System.out.println(color + "的" + breed + "正在吃鱼.....");
}
public void catchMouse() {
System.out.println(color + "的" + breed + "正在抓老鼠.....");
}
}
package cn.kox.homework.four;
/**
* @ClassName: Dog
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class Dog {
private String color;
private String breed;
public Dog(String color, String breed) {
this.color = color;
this.breed = breed;
}
public void eat() {
System.out.println(color + "的" + breed + "正在吃饭.....");
}
public void lookHome() {
System.out.println(color + "的" + breed + "正在看家.....");
}
}
package cn.kox.homework.four;
/**
* @ClassName: Demo
* @Author: Kox
* @Data: 2023/1/19
* @Sketch:
*/
public class Demo {
public static void main(String[] args) {
Cat c1 = new Cat();
c1.setColor("狸花猫");
c1.setBreed("灰色");
c1.catchMouse();
c1.eat();
Dog d1 = new Dog("橙色", "金毛");
d1.eat();
d1.lookHome();
}
}