面向对象练习
找对象
以下名词中哪些是对象?
- 猫
- 狗
- Tom
- Jerry
- 人
- 帅哥
- 美女
- 迪丽热巴
- 胡歌
- Phone
- iPhone 14 Pro Max
- 《Java面向对象》
编程
**1、**编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出
package com.kfm.oopDemo;
public class Student {
//编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
// 类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
// 在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出
static String name = "张三";
String gender = "男";
int age = 3;
int id = 01;
double score = 89;
public void say(){
System.out.printf("我是%s,性别%s,今年%d岁,ID为%d的成绩是%.2f。",name,gender,age,id,score);
}
}
package com.kfm.oopDemo;
public class StudentTest {
public static void main(String[] args) {
Student zs = new Student();
//在这里输入信息也可以
zs.say();
}
}
**2、**定义一个女朋友类(GirlFriend)女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
我女朋友叫凤姐,身高155.0厘米,体重130.0斤
女朋友帮我洗衣服
女朋友给我做饭
package com.kfm.oopDemo;
public class GirlFriend {
//定义一个女朋友类(GirlFriend)女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。
// 另外定义一个用于展示三个属性值的show()方法。
// 请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。打印效果如下:
//我女朋友叫凤姐,身高155.0厘米,体重130.0斤
//女朋友帮我洗衣服
//女朋友给我做饭
String name;
double height;
double weight;
public void wash(){
System.out.println("女朋友帮我洗衣服");
}
public void cook(){
System.out.println("女朋友帮我做饭");
}
public void show(){
System.out.printf("我的女朋友叫%s,身高%.1f厘米,体重%.1f斤\n",name,height,weight);
}
}
package com.kfm.oopDemo;
public class GirlFriendTest {
public static void main(String[] args) {
GirlFriend fj = new GirlFriend();
fj.name = "凤姐";
fj.height = 155.0;
fj.weight = 130.0;
fj.show();
fj.wash();
fj.cook();
}
}
**3、**定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse()定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家()
要求:
1.按照以上要求定义Cat类和Dog类,属性要私有,生成无参、有参构造
2.定义测试类,在main方法中创建该类的对象并给属性赋值
3.调用成员方法,打印格式如下:
花色的波斯猫正在吃鱼…
花色的波斯猫正在逮老鼠…
黑色的藏獒正在啃骨头…
黑色的藏獒正在看家…
package com.kfm.oopDemo;
public class Cat {
//定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse()
// 定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家()
//要求:
//1.按照以上要求定义Cat类和Dog类,属性要私有,生成无参、有参构造
//2.定义测试类,在main方法中创建该类的对象并给属性赋值
//调用成员方法,打印格式如下:
//花色的波斯猫正在吃鱼.....
//花色的波斯猫正在逮老鼠....
//黑色的藏獒正在啃骨头.....
//黑色的藏獒正在看家.....
public String color;
public String breed;
//无参
public void eat(){
System.out.printf("%s的%s正在吃鱼...\n",color,breed);
}
//有参
public void catchMouse(String content){
System.out.println(this.color + "的" + this.breed + content);
}
}
package com.kfm.oopDemo;
public class Dog {
public String color;
public String breed;
//无参
public void eat(){
System.out.printf("%s的%s正在啃骨头...\n",color,breed);
}
//有参
public void see(String content1){
System.out.println(this.color + "的" + this.breed + content1);
}
}
package com.kfm.oopDemo;
public class CatTest {
public static void main(String[] args) {
Cat cat = new Cat();
cat.color = "花色";
cat.breed = "波斯猫";
Dog dog = new Dog();
dog.color = "黑色";
dog.breed = "藏獒";
cat.eat();
cat.catchMouse("正在抓老鼠...");
dog.eat();
dog.see("正在看家...");
}
}