一、多态编程
Person类具有String属性name(姓名)和display()方法;
Teacher类继承Person类,并具有String属性professionalTitle(职称)和display()方法;
Student类继承Person类,并具有int属性studentNumber(学号)和display()方法;
Graduate类和Undergraduate类都继承Student类,并具有各自的display()方法,
Graduate类具有String属性major(专业),
Undergraduate类具有int属性grade(年级)。
所有类都应该具有合适的构造方法和按照一定格式(见输出样例)输出所有属性值display()方法。
要求编程实现这些类,使给定的Test类能正常运行,并实现指定的输出内容。
Test类代码如下:
public class Test
{
public static void main(String[] args)
{
Person[] people = new Person[5];
people[0] = new Person("Tom");
people[1] = new Teacher("Kevin","professor");
people[2] = new Student("Jerry",21);
people[3] = new Undergraduate("John",22,3);
people[4] = new Graduate("Mary",23,"computer");
for (Person p: people)
{
p.display();
System.out.println(">>>");
}
}
}
【输入形式】
【输出形式】
【样例输入】
【样例输出】
Name: Tom
>>>
Name: Kevin
Professional Title: professor
>>>
Name: Jerry
StudeneNumber: 21
>>>
Name: John
StudeneNumber: 22
Grade: 3
>>>
Name: Mary
StudeneNumber: 23
Major: computer
>>>
public class Test {
public static void main(String[] args) {
Person1[] people = new Person1[5];
people[0] = new Person1("Tom");
people[1] = new Teacher1("Kevin", "professor");
people[2] = new Sudent("Jerry", 21);
people[3] = new Underraduate("John", 22, 3);
people[4] = new Gaduate("Mary", 23, "computer");
for (Person1 p : people) {
p.display();
System.out.println(">>>");
}
}
}
class Person1 {
protected String name;
public Person1(String name) {
this.name = name;
}
public void display() {
System.out.println("Name: " + name);
}
}
class Teacher1 extends Person1 {
private String professionalTitle;
public Teacher1(String name, String professionalTitle) {
super(name);
this.professionalTitle = professionalTitle;
}
public void display() {
super.display();
System.out.println("Professional Title: " + professionalTitle);
}
}
class Sudent extends Person1 {
private int studentNumber;
public Sudent(String name, int studentNumber) {
super(name);
this.studentNumber = studentNumber;
}
public void display() {
super.display();
System.out.println("StudeneNumber: " + studentNumber);
}
}
class Gaduate extends Sudent {
private String major;
public Gaduate(String name, int studentNumber, String major) {
super(name, studentNumber);
this.major = major;
}
public void display() {
super.display();
System.out.println("Major: " + major);
}
}
class Underraduate extends Sudent {
private int grade;
public Underraduate(String name, int studentNumber, int grade) {
super(name, studentNumber);
this.grade = grade;
}
public void display() {
super.display();
System.out.println("Grade: " + grade);
}
}
二、多态编程2
Animal类具有eat(Food food)方法,功能是输出形如"It's time to eat Fish."的语句,其中“Fish”是food对象的类名。
Dog类需要重写Object类的toString()方法,返回类名字符 串;还要重写Animal类的eat()方法,增加输出形如“Dog like Bone”语句的功能,其中“Bone”是food对象的类名。
Cat类与Dog类相似;
Food类具有weight属性,和适当的构造方法和置取方法;
Bone类和Fish类都需要重写Object类的toString()方法,返回类名字符串;
要求编程实现这些类,使给定的Test类能正常运行,并实现指定的输出内容(Feeder类已给出)。
public class Test{
public static void main(String[] args) {
Feeder fd = new Feeder("Tom");
Dog dog = new Dog();
Bone bone = new Bone(500);
fd.feed(dog, bone);
System.out.println("-----------------");
fd.feed(new Cat(), new Fish(100));
}
}
class Feeder {
private String name;
public Feeder(String name) {
this.name = name;
}
public void feed(Animal a, Food f) {
a.eat(f);
System.out.println("Feeder "+name+" feed "+a+ " with "+f.getWeight()+"g "+f+".");
}
}
【输入形式】
【输出形式】
It's time to eat Bone.
Dog like Bone
Feeder Tom feed Dog with 500g Bone.
-----------------
It's time to eat Fish.
Cat like Fish
Feeder Tom feed Cat with 100g Fish.
public class Test{
public static void main(String[] args) {
Feeder fd = new Feeder("Tom");
Dog dog = new Dog();
Bone bone = new Bone(500);
fd.feed(dog, bone);
System.out.println("-----------------");
fd.feed(new Cat(), new Fish(100));
}
}
class Feeder {
private String name;
public Feeder(String name) {
this.name = name;
}
public void feed(Animal a, Food f) {
a.eat(f);
System.out.println("Feeder " + name + " feed " + a + " with " + f.getWeight() + "g " + f + ".");
}
}
class Animal {
public void eat(Food food) {
System.out.println("It's time to eat " + food + ".");
}
}
class Dog extends Animal {
public void eat(Food food) {
super.eat(food);
System.out.println("Dog like " + food);
}
public String toString() {
return "Dog";
}
}
class Cat extends Animal {
public void eat(Food food) {
super.eat(food);
System.out.println("Cat like " + food);
}
public String toString() {
return "Cat";
}
}
class Food {
private int weight;
public Food(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public String toString() {
return this.getClass().getSimpleName();
}
}
class Bone extends Food {
public Bone(int weight) {
super(weight);
}
public String toString() {
return "Bone";
}
}
class Fish extends Food {
public Fish(int weight) {
super(weight);
}
public String toString() {
return "Fish";
}
}
三、根据名字判断两个人是否是同一个人
【问题描述】定义Person类,重写equals方法,根据姓名判断两个是否是同一个人
【输入形式】无
【输出形式】判断结果
【样例输入】无
【样例输出】true
【样例说明】
【评分标准】
public class Test{
public static void main(String[] args)
{
Person p1 = new Person("张三", 20);
Person p2 = new Person("张三", 20);
System.out.println(p1.equals(p2));
}
}
class Person{
private String name;
private int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public boolean equals(Object obj){
}
}
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
四、显示手机信息
设计手机类MobilePhone,继承类Phone ,显示手机的品牌、号码和机主ID。同时定义测试类Test如下:
public class Test{
public static void main(String [] args){
Phone phone=new MobilePhone("HUAWEI","13899999999","130111111111111111");
phone.display();
}
}
增加MobilePhone类,运行Test类。
【输入形式】无
【输出形式】
Code=13899999999
Brand=HUAWEI
Ownerld=130111111111111111
public class Test {
public static void main(String[] args) {
Phone phone = new Mobilphone("HUAWEI", "13899999999", "130111111111111111");
phone.display();
}
}
class Phone {
protected String number;
public Phone(String number) {
this.number = number;
}
public void display() {
System.out.println("Code=" + number);
}
}
class Mobilphone extends Phone {
private String brand;
private String ownerID;
public Mobilphone(String brand, String number, String ownerID) {
super(number);
this.brand = brand;
this.ownerID = ownerID;
}
public void display() {
super.display();
System.out.println("Brand=" + brand);
System.out.println("Ownerld=" + ownerID);
}
}
五、 显示Person子类学生类信息
设计学生类Student,继承类Person,显示学生姓名和成绩。同时定义测试类Test如下:
public class Test{
public static void main(String [] args){
Student s = new Student("lisi", 23, 86);
s.display();
}
}
增加Student类,运行Test类。
【输入形式】无
【输出形式】
Name=lisi
Grade=86.0
public class Test
{
public static void main(String [] args){
Studet s = new Studet("lisi", 23, 86);
s.display();
}
}
class person {
protected String name;
protected int age;
// protected double grade;
public person(String name,int age){
this.name=name;
this.age=age;
// this.grade=grade;
}
// public void display(){
// System.out.println("Nmae="+name);
// }
}
class Studet extends person{
private double grade;
public Studet(String name,int age, double grade){
super(name , age);
this.grade=grade;
}
public void display(){
System.out.println("Name="+name);
System.out.println("Grade="+grade);
}
}
六、显示Person类子类工人信息
设计工人类Worker,继承类Person,显示工人的姓名、工资和级别。同时定义测试类Test如下:
public class Test{
public static void main(String [] args){
Person w = new Worker("zhang",45,4560,1);
w.display();
}
}
增加Worker类,运行Test类。
【输入形式】无
【输出形式】
Name=zhang
Salary=4560.0
Level=1
public class Test{
public static void main(String [] args){
Perso w = new Worke("zhang",45,4560,1);
w.display();
}
}
class Perso{
protected String name;
protected int age;
public Perso(String name,int age){
this.name=name;
this.age=age;
}
public void display(){
System.out.println("Name="+name);
}
}
class Worke extends Perso{
private double salay;
private int level;
public Worke(String name,int age, double salay ,int level){
super(name,age);
this.salay=salay;
this.level=level;
}
public void display(){
super.display();
System.out.println("Salary="+salay);
System.out.println("Level="+level);
}
}
七、显示桌子子类方桌信息
设计方桌类RectangleTable,继承类TableInfo ,显示桌子的腿数、类型、长和宽。同时定义测试类Test如下:
public class Test{
public static void main(String[] args)
{
TableInfo t2=new RectangleTable(4, 100, 40.0, 60.0);
t2.print();
}
}
增加RectangleTable类,运行Test类。
【输入形式】无
【输出形式】
Legs=4
Rect
Len=60.0,Width=40.0
public class Test{
public static void main(String[] args)
{
TableInfo t2=new RectangleTable(4, 100, 60.0, 40.0);
t2.print();
}
}
class TableInfo{
protected int legs;
protected String typs;
public TableInfo(int legs,String typs){
this.legs=legs;
this.typs=typs;
}
public void print(){
System.out.println("Legs="+legs);
System.out.println(typs);
}
}
class RectangleTable extends TableInfo{
private double len;
private double width;
public RectangleTable(int legs, int typs, double len, double width){
super(legs,"Rect");
this.len=len;
this.width=width;
}
public void print(){
super.print();
System.out.println("Len="+len+","+"Width="+width);
}
}
希望大家多多点赞,多多关注,这样我会尽最大的努力去发布最新的Java编程。谢谢!