1903021104-褚晓娜-java第十一周作业-多态、抽象类的运用

项目 内容
课程这个作业要求链接 19级信计班(本)
这个作业要求链接 java第十一周作业
博客名称: 1903021104-褚晓娜-java第十一周作业-多态、抽象类的运用
*要求: 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)。

题目1:

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

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

package edu.xj.cxn.week11;

public class Person {
	String name ;        
	int birthYear ;
	public Person(String name, int birthYear) {
	super();
	this.name = name;
	this.birthYear = birthYear;
	 }
	 public String getMessage() {
	   return "姓名:" +name;
	   }
	
	}


**子类:**
package edu.xj.cxn.week11;

public class Graduate extends Person {
	int gpa;
	int graduate;
	int nowYear;
	public Graduate(String name, int birthYear, int gpa, int graduate,
			int nowYear) {
		super(name, birthYear);
		this.gpa = gpa;
		this.graduate = graduate;
		this.nowYear = nowYear;
		

	}
	public String getMessage() {
     return super.getMessage()+"\n成绩:"+gpa+"\n毕业年份:"+graduate;
	}
	public int age(){
		return nowYear-birthYear;
		
	}
}

**测试类:**
package edu.xj.cxn.week11;

public class Text {

	public static void main(String[] args) {
		 Graduate g = new Graduate("王怡",2000,90,2023,2022);
		 System.out.println(g.getMessage());
		 System.out.println("年龄:"+g.age());
		// TODO Auto-generated method stub

	}

}

截图:

题目2:

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

代码:

package edu.xj.cxn.week11;

public class Shape {
	void draw(){

}
}

**Circle类:**
package edu.xj.cxn.week11;

public class Circle extends Shape {
	void draw(){

	System.out.println("Circle.draw()");
}
}

**Triangle类:**
package edu.xj.cxn.week11;

public class Triangle extends Shape {
	void draw(){

		System.out.println("Triangle.draw()");
	}

}

**Square类:**
package edu.xj.cxn.week11;

public class Square extends Shape {
	void draw(){

		System.out.println("Square.draw()");
	}

}

**测试类:**
package edu.xj.cxn.week11;

public class Text2 {
	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);
		// TODO Auto-generated method stub

	}

}

截图:

题目3:

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

提示:抽象

代码:

package edu.xj.cxn.week11;

public class Animal {
	void bark(){
		
	}

}


**Dog子类:**
package edu.xj.cxn.week11;

public class Dog extends Animal {
	void bark(){

		System.out.println("狗的叫声是:汪汪汪~");
	}

}


**Bird子类:**
package edu.xj.cxn.week11;

public class Bird {
	void bark(){

		System.out.println("鸟的叫声是:叽叽喳喳");
	}


}


**测试类:**
package edu.xj.cxn.week11;

public class Text3 {

	public static void main(String[] args) {
		Dog d=new Dog();
		Bird b=new Bird();
		d.bark();
		b.bark();
		// TODO Auto-generated method stub

	}

}

截图:

题目4:

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

提示:抽象

代码:

package edu.xj.cxn.week11;

public  abstract class Xingzhuang {
int width;
int height;
public Xingzhuang(int width, int height) {
	super();
	this.width = width;
	this.height = height;
}
public  abstract double area();
}


**Trigon子类:**
package edu.xj.cxn.week11;

public class Trigon extends Xingzhuang {

	public Trigon(int width, int height) {
		super(width, height);
		// TODO Auto-generated constructor stub
	}
	public double area(){
		return (width*height)/2;
}
}

**Rectangle子类:**
package edu.xj.cxn.week11;

public  class Rectangle extends Xingzhuang {

	public Rectangle(int width, int height) {
		super(width, height);
		// TODO Auto-generated constructor stub
	}
 public double area(){
	return width*height;
}
}


**测试类:**
package edu.xj.cxn.week11;

public class Text4 {

	public static void main(String[] args) {
		Rectangle r=new Rectangle(6,5);
		System.out.println("长方形的面积为:"+r.area());
	    Trigon t=new Trigon(4,6);
	    System.out.println("三角形的面积为:"+t.area());
		// TODO Auto-generated method stub

	}

}

截图:

心得体会:
1、继承的概念:在已经存在的类的基础上进行扩充和改造,形成一个新的类。满足“is a”的关系就是继承。

2、多态的概念:多态是一种事务,多种形态。用名字一样的代码根据要求的不同,干不一样的事情。

   分类:

             (1)对象的多态。(父对象引用指向子类实例)

             (2)方法的多态。(分为方法的重载和重写或者覆盖)

3、方法的重载:重载是发生在同一类中,函数的名字相同,但是函数的参数个数,和参数类型,只要不一样,就是重载。

4、抽象类:

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

类:class关键字创建

抽象类: 通过abstract 修饰的java类就是。

posted on 2022-05-13 22:46  糖果&不甜  阅读(45)  评论(0编辑  收藏  举报

导航