哈尔滨网站优化排名,大连网站开发招聘,网站开发方向,wordpress去除tag知识点
面向对象
题目1#xff08;完成#xff09;
定义手机类#xff0c;手机有品牌(brand),价格(price)和颜色(color)三个属性#xff0c;有打电话call()和sendMessage()两个功能。
请定义出手机类#xff0c;类中要有空参、有参构造方法#xff0c;set/get方法。 …知识点
面向对象
题目1完成
定义手机类手机有品牌(brand),价格(price)和颜色(color)三个属性有打电话call()和sendMessage()两个功能。
请定义出手机类类中要有空参、有参构造方法set/get方法。
定义测试类在主方法中使用空参构造创建对象使用set方法赋值。
调用对象的两个功能打印效果如下
正在使用价格为3998元黑色的小米手机打电话....
正在使用价格为3998元黑色的小米手机发短信....
训练提示 类中的属性就是成员变量类中的行为功能就是成员方法。 成员变量要被private修饰。
解题方案
操作步骤 定义手机类手机类中定义String类型的品牌int类型的价格String类型的颜色三个成员变量都用privice修饰。 提供空参构造方法和有参构造方法。 提供set/get方法。 编写打电话的成员方法方法中对成员变量进行使用。 编写发短信的成员方法方法中对成员变量进行使用。 在测试类中创建手机对象使用set方法赋值分别调用各个方法。
参考答案
package test;
public class Phone {
private String brand;private int price;private String color;
public Phone() {}
public Phone(String brand, int 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 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 test;
public class PhoneTest {public static void main(String[] args) {Phone phone new Phone();
phone.setBrand(小米);phone.setPrice(3998);phone.setColor(黑色);
phone.call();phone.sendMessage();
}
}
题目2完成
定义一个女朋友类。女朋友的属性包含姓名身高体重。行为包含洗衣服wash()做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下
我女朋友叫小希,身高162.0厘米,体重100.0斤
女朋友帮我洗衣服
女朋友给我做饭
训练提示 类中的属性就是成员变量类中的行为功能就是成员方法。 成员变量要被private修饰。 展示方法的作用就是打印姓名、身高、体重三个成员变量的值。
解题方案
操作步骤 定义女朋友类定义String类型姓名double类型身高和double类型体重三个成员变量三个成员变量都用privice修饰。 提供空参构造方法和有参构造方法。 提供set/get方法。 编写展示方法show()方法打印三个成员变量的值。 编写洗衣服wash()方法输出洗衣服的语句。 编写做饭cook()方法输出做饭的语句。 在测试类中使用有参构造创建女友对象分别调用各个方法。
参考答案
package test.test2;
public class GirlFriend {private String name;private double height;private double jin;
public GirlFriend() {}
public GirlFriend(String name, double height, double jin) {this.name name;this.height height;this.jin jin;}
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 getJin() {return jin;}
public void setJin(double jin) {this.jin jin;}
public void show() {System.out.println(我女朋友叫 name ,身高 height 厘米,体重 jin 斤);}
public void wash() {System.out.println(女朋友帮我洗衣服);}
public void cook() {System.out.println(女朋友给我做饭);}
}
package test.test2;
public class GirlFriendTest {public static void main(String[] args) {GirlFriend g new GirlFriend(小希, 162, 100);
g.show();g.wash();g.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 test.test3;
public class Manager {private String name;private int id;private int salary;private int bonus;
public Manager() {}
public Manager(String name, int id, int salary, int bonus) {this.name name;this.id id;this.salary salary;this.bonus bonus;}
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 void work() {System.out.println(工号为 id 基本工资为 salary 奖金为 bonus 的项目经理 name 正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....);}
}
package test.test3;
public class Coder {private String name;private int id;private int salary;
public Coder() {}
public Coder(String name, int id, int salary) {this.name name;this.id id;this.salary salary;}
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 void work() {System.out.println(工号为 id 基本工资为 salary 的程序员 name 正在努力的写着代码......);}
}
package test.test3;
public class Test {public static void main(String[] args) {Manager m new Manager(张三, 123, 15000, 6000);Coder c new Coder(李四, 135, 10000);
m.work();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 test.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 test.test4;
public class Dog {private String color;private 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 test.test4;
public class Test {public static void main(String[] args) {Cat c new Cat(花色, 波斯猫);c.eat();c.catchMouse();
Dog d new Dog(黑色, 藏獒);d.eat();d.lookHome();
}
}