知识点
面向对象
题目1(完成)
定义手机类,手机有品牌(brand),价格(price)和颜色(color)三个属性,有打电话call()和sendMessage()两个功能。
请定义出手机类,类中要有空参、有参构造方法,set/get方法。
定义测试类,在主方法中使用空参构造创建对象,使用set方法赋值。
调用对象的两个功能,打印效果如下:
正在使用价格为3998元黑色的小米手机打电话.... 正在使用价格为3998元黑色的小米手机发短信....
训练提示
-
类中的属性就是成员变量,类中的行为功能就是成员方法。
-
成员变量要被private修饰。
解题方案
操作步骤
-
定义手机类,手机类中定义String类型的品牌,int类型的价格,String类型的颜色,三个成员变量都用private修饰。
-
提供空参构造方法和有参构造方法。
-
提供set/get方法。
-
编写打电话的成员方法,方法中对成员变量进行使用。
-
编写发短信的成员方法,方法中对成员变量进行使用。
-
在测试类中创建手机对象,使用set方法赋值,分别调用各个方法。
参考答案
package day08.test1;
public class Phone {
//属性
private String brand;
private int price;
private String color;
//构造方法
public String getBrand() {
return brand;
}
public Phone() {
}
//get/set方法
public Phone(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//方法
public void call() {
System.out.println("正在使用价格为" + price + "元" + color + "色的" + brand + "手机打电话....");
}
public void sendMessage() {
System.out.println("正在使用价格为" + price + "元" + color + "色的" + brand + "手机发短信....");
}
}
package day08.test1;
public class Test {
public static void main(String[] args) {
Phone p = new Phone();
p.setBrand("小米");
p.setColor("黑");
p.setPrice(3998);
p.call();
p.sendMessage();
}
}
题目2(完成)
定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
我女朋友叫凤姐,身高155.0厘米,体重130.0斤 女朋友帮我洗衣服 女朋友给我做饭
训练提
-
类中的属性就是成员变量,类中的行为功能就是成员方法。
-
成员变量要被private修饰。
-
展示方法的作用就是打印姓名、身高、体重三个成员变量的值。
解题方案
操作步骤
-
定义女朋友类,定义String类型姓名,double类型身高和double类型体重三个成员变量,三个成员变量都用privice修饰。
-
提供空参构造方法和有参构造方法。
-
提供set/get方法。
-
编写展示方法show(),方法打印三个成员变量的值。
-
编写洗衣服wash()方法,输出洗衣服的语句。
-
编写做饭cook()方法,输出做饭的语句。
-
在测试类中使用有参构造创建女友对象,分别调用各个方法。
参考答案
package day08.test2;
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 wash() {
System.out.println("女朋友帮我洗衣服");
}
public void cook() {
System.out.println("女朋友给我做饭");
}
public void show() {
System.out.println("我女朋友叫" + name + ",身高" + height + "厘米,体重" + weight + "斤");
}
}
package day08.test2;
public class Test {
public static void main(String[] args) {
GirlFriend girlFriend = new GirlFriend("凤姐", 155.0, 130);
girlFriend.show();
girlFriend.wash();
girlFriend.cook();
}
}
题目3(完成)
定义项目经理类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的程序员李四正在努力的写着代码......
训练提示
-
类中的属性就是成员变量,类中的行为功能就是成员方法。
-
成员变量要被private修饰。
-
在工作work()方法中调用成员变量,输出成员变量的值。
解题方案
操作步骤
-
定义项目经理类,定义成员变量,构造方法,set和get方法,work方法,方法中根据打印格式输出id,salary,bonus的值。
-
定义程序猿类,定义成员变量,构造方法,set和get方法,work方法,方法中根据打印格式输出id和salary的值。
-
在测试类中使用有参构造创建项目经理对象并赋值,调用工作方法打印结果。
-
在测试类中使用有参构造创建程序员对象并赋值,调用工作方法打印结果。
参考答案
package day08.test3;
public class Manager {
private String id;
private String name;
private double salary;
private double bonus;
public Manager() {
}
public Manager(String id, String name, double salary, double bonus) {
this.id = id;
this.name = name;
this.salary = salary;
this.bonus = bonus;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public void work() {
System.out.println("工号为" + id + "基本工资为" + salary + "奖金为" + bonus + "的项目经理"+name+
"正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
}
package day08.test3;
public class Coder {
private String id;
private String name;
private double salary;
public Coder() {
}
public Coder(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work() {
System.out.println("工号为" + id + "基本工资为" + salary + "的程序员"+name+"正在努力的写着代码......");
}
}
package day08.test3;
public class Test {
public static void main(String[] args) {
Manager m = new Manager("123","张三",15000,6000);
m.work();
Coder c = new Coder("135","李四",10000);
c.work();
}
}
题目4(完成)
定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse() 定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家lookHome() 要求: 1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,set和get方法 2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法) 3.调用成员方法,打印格式如下:
花色的波斯猫正在吃鱼..... 花色的波斯猫正在逮老鼠.... 黑色的藏獒正在啃骨头..... 黑色的藏獒正在看家.....
训练提示
-
类中的属性就是成员变量,类中的行为功能就是成员方法。
-
成员变量要被private修饰。
解题方案
操作步骤
-
定义猫类,定义成员变量,构造方法,set和get方法,吃饭方法eat(),抓老鼠方法catchMouse(),方法中根据题目给出的格式输出成员变量的值。
-
定义狗类,定义成员变量,构造方法,set和get方法,吃饭方法eat(),看家方法lookHome(),方法中根据题目给出的格式输出成员变量的值。
-
在测试类中使用有参构造创建猫类对象,调用eat()方法和catchMouse()方法。
-
在测试类中使用有参构造创建狗类对象,调用eat()方法和lookHome()方法。
参考答案
package day08.test4;
public class Cat {
private String color;
private String breed;
public Cat() {
}
public Cat(String color, String breed) {
this.color = color;
this.breed = breed;
}
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 day08.test4;
public class Dog {
public String color;
public String breed;
public Dog() {
}
public Dog(String color, String breed) {
this.color = color;
this.breed = breed;
}
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 lookHome() {
System.out.println(color + "的" + breed + "正在看家.....");
}
}
package day08.test4;
public class Test {
public static void main(String[] args) {
Cat cat=new Cat("花色","波斯猫");
cat.eat();
cat.catchMouse();
Dog dog=new Dog("黑色","藏獒");
dog.eat();
dog.lookHome();
}
}