一、实验目的和要求
(1) 熟练运用Java语言进行面向对象程序设计;
(2) 深入理解面向对象的封装、继承和多态等特性,掌握类、继承、包和接口的应用;
(3) 能熟练应用方法、类、成员变量等元素;
(4) 了解并掌握抽象类和接口概念和定义;
(5)掌握关键字static和final的使用;
二、实验内容
(1) 编写应用程序,具体要求如下:
① 声明一个抽象类Pet,封装属性name和sex,声明一个带有两个参数的构造函数,声明抽象方法void talk()和void eat();
② 声明一个Dog类继承自Pet,封装属性color,声明带有三个参数的构造函数,复写talk()和eat()方法;
③ 声明一个Cat类继承自Pet,封装属性weight,声明带有三个参数的构造函数,复写talk()和eat()方法;
④ 编写测试类,通过有参构造函数实例化Dog类对象,调用talk()方法和eat()方法;通过有参构造函数实例化Cat类对象,调用talk()方法和eat()方法;
(2) 按照要求编写一个Java应用程序:
① 定义一个抽象类Person,包含抽象方法eat(),封装属性name、sex、age,声明包含三个参数的构造方法;
② 定义一个Chinese类,继承自Person类,重写父类的eat()方法,并定义一个自己特有的方法shadowBoxing();
③ 定义一个English类,继承自Person类,重写父类的eat()方法,并定义一个自己特有的方法horseRiding();
编写测试类,定义一个showEat()方法,使用父类作为方法的形参,实现多态,分别调用showEat()方法,通过强制类型转换调用各自类特有的方法;
三、实验记录
(一)public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String dogName = sc.next();
String dogSex = sc.next();
String dogColor = sc.next();
String catName = sc.next();
String catSex = sc.next();
double catWeight = sc.nextDouble();
Dog dog=new Dog(dogName,dogSex,dogColor);
dog.talk();
dog.eat();
Cat cat=new Cat(catName,catSex,catWeight);
cat.talk();
cat.eat();
}
}
abstract class Pet {
String name;
String sex;
abstract void talk();
abstract void eat();
}
class Dog extends Pet {
String color;
Dog(String name,String sex,String color){
this.name=name;
this.sex=sex;
this.color=color;
}
void talk(){
System.out.println("名称:"+name+",性别:"+sex+",颜色:"+color+",汪汪叫");
}
void eat(){
System.out.println(name+"吃骨头!");
}
}
class Cat extends Pet {
double weight;
Cat(String name,String sex,double weight){
this.name=name;
this.sex=sex;
this.weight=weight;
}
void talk(){
System.out.println("名称:"+name+",性别:"+sex+",体重:"+weight+"kg,喵喵叫");
}
void eat(){
System.out.println(name+"吃鱼!");
}
}
(二)public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String cName = sc.next();
String cSex = sc.next();
int cAge = sc.nextInt();
String eName = sc.next();
String eSex = sc.next();
int eAge = sc.nextInt();
Person person1=new Chinese(cName,cSex,cAge);
showEat(person1);
Person person2=new English(eName,eSex,eAge);
showEat(person2);
Chinese c=(Chinese)person1;
c.shadowBoxing();
English e=(English)person2;
e.horseRiding();
}
public static void showEat(Person p){
p.eat();
}
}
abstract class Person {
public String name;
public String sex;
public int age;
abstract void eat();
}
class Chinese extends Person {
public String name;
public String sex;
public int age;
Chinese(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
void eat(){
System.out.println("姓名:"+name+",性别:"+sex+",年龄:"+age+",我是中国人,我喜欢吃饭!");
}
void shadowBoxing(){
System.out.println(name+"在练习太极拳!");
}
}
class English extends Person {
public String name;
public String sex;
public int age;
English(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
void eat(){
System.out.println("姓名:"+name+",性别:"+sex+",年龄:"+age+",我是英国人,我喜欢吃三明治!");
}
void horseRiding(){
System.out.println(name+"在练习骑马!");
}
}
本文来自博客园,作者:一路向北~~,转载请注明原文链接:https://www.cnblogs.com/ylxb2539989915/p/16338595.html