沈么是快乐星球

1903021111—郭付琴—Java第十一周作业—继承、多态和抽象类的应用

项目 内容
课程班级博客链接 19级信计班
这个作业要求链接 第十一周作业
博客名称 1903021111—郭付琴—Java第十一周作业—继承、多态和抽象类的应用
要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)

题目1:

  • 类Person定义了姓名name,出生年份birthYear,其子类Graduate在继承父类的基础上新增定义了成绩gpa、毕业年份graduateYear。编写测试类,输出毕业生的姓名、年龄、毕业年份、成绩。

  • 提示:父类要有构造方法,子类通过super调用父类构造方法。

代码:

  • 父类:
package xjweek11;

public class Person {
		String name;
	    int birthYear;
		public Person(String name, int birthYear) {
			super();
			this.name = name;
			this.birthYear = birthYear;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getBirthYear() {
			return birthYear;
		}
		public void setBirthYear(int birthYear) {
			this.birthYear = birthYear;
		}
		
	}
  • 子类:
package xjweek11;

public class Graduate extends Person {
	int gap;
	int graduateYear; 
	public Graduate(String name, int birthYear, int gap, int graduateYear) {
		super(name, birthYear);
		this.gap = gap;
		this.graduateYear = graduateYear;
	}
	public int getGap() {
		return gap;
	}
	public void setGap(int gap) {
		this.gap = gap;
	}
	public int getGraduateYear() {
		return graduateYear;
	}
	public void setGraduateYear(int graduateYear) {
		this.graduateYear = graduateYear;
	}	
	
}
  • 测试类:
package xjweek11;

public class GraduateTest {
	public static <Greduate> void main(String[] args) {
		Graduate G = new Graduate("唐三",1991,100,2014);
		System.out.println("姓名:"+G.getName()+"\n年龄:"+(G.getGraduateYear()-G.getBirthYear())+"\n毕业年份:"+G.getGraduateYear()+"\n成绩:"+G.getGap());
	}
}

 运行结果:

题目2:

  • 定义一个基本类Shape,有一个draw方法,定义三个类Circle(圆)、Triangle(三角形)、Square(正方形)都继承于Shape。在测试类中定义一个方法doStuff,传入基本类shape为参数,并调用draw方法。使用父类Shape创建三个类Circle、 Triangle、Square的实例对象并分别作为参数传入draw方法中。
  • 提示:多态

代码:

  • 父类:
package xjweek11;

public class Shape {
		void draw(){
	}
}
  • 子类:
package xjweek11;

public class Circle extends Shape {
	void draw(){
		System.out.println("Circle.draw()");
	}
}
package xjweek11;

public class Triangle extends Shape {
	void draw(){
		System.out.println("Triangle.draw()");
	}
}
package xjweek11;

public class Square extends Shape {
	void draw(){
		System.out.println("Square.draw()");
	}
}
  • 测试类:
package xjweek11;

public class ShapeTest {
	static void doStuff(Shape s){
		s.draw();
	}
	public static void main(String[] args) {
		Shape c=new Circle();
		Shape s=new Square();
		Shape t=new Triangle();
		doStuff(c);
		doStuff(s);
		doStuff(t);
	}
}

运行结果:

 

题目3:

  • 所有的动物都有一个父类Animal,再定义两个子类Bird(鸟)和Dog(狗)继承自Animal,并实现父类中的bark(叫唤)方法。

  • 提示:抽象

代码:

  • 父类:
package xjweek11;

public class Animal {
void bark() {
	}
}
  • 子类:
package xjweek11;

public class Bird extends Animal {
	void bark(){
		System.out.println("鸟的叫声是:叽叽喳喳");
	}
}
package xjweek11;

public class Dog extends Animal {
	void bark(){
		System.out.println("狗的叫声是:汪汪汪");
	}
}
  • 测试类:
package xjweek11;

public class AnimalTest {
	public static void main(String[] args) {
		Bird b=new Bird();
		Dog d=new Dog();
		b.bark();
		d.bark();
	}
}

运行结果:

题目4:

  • 不同几何图形的面积计算公式是不一样的,可是,它们具有的特性是一样的,都具有长和宽这两个属性,也都具有面积计算的方法。根据抽象的概念计算长方形的面积和三角形的面积。

  • 提示:抽象

代码:

  • 父类:
package xjweek11;

public abstract class Shape {
	abstract void Area();
}
  • 子类:
package xjweek11;

public class Rectangle extends Shape {

	int lenght;
	int height;
	public Rectangle(int lenght, int height) {
		super();
		this.lenght = lenght;
		this.height = height;
	}
	void Area() {
		System.out.println("长方形的面积是:"+lenght*height);
	}
}
package xjweek11;

public class Triangle extends Shape {
	int lenght;
	int height;
	public Triangle(int lenght, int height) {
		super();
		this.lenght = lenght;
		this.height = height;
	}
	void Area() {
		System.out.println("三角形的面积是:"+lenght*height/2);
	}
}
  • 测试类:
package xjweek11;

public class ShapeTest {
	public static void main(String[] args) {
		Rectangle R = new Rectangle(1,9);
		R.Area();
		Triangle T = new Triangle(9,1);
		T.Area();
	}
}

运行结果:

                    2、多态是一种事务,多种形态。用名字一样的代码根据要求的不同,干不一样的事情。分类:对象的多态。(父对象引用指向子类实例)方法的多态。(分为方法的重载和重写或者覆盖)

                    3、类:用来描述具有共同性质的一组事物的自定义复合数据类型。

posted on 2022-05-15 19:25  沈么是快乐星球  阅读(48)  评论(0编辑  收藏  举报

导航