Java,多态示例,继承与多态,

发布于:2024-04-17 ⋅ 阅读:(25) ⋅ 点赞:(0)
package co7;
import java.util.*;
class Shape{
	void draw() {}
	void erase() {}
}
class Circle extends Shape{
	void draw() {
		System.out.println("Calling Circle.draw()");
	}
	void erase() {
		System.out.println("Calling Circle.erase()");
	}
}
class Square extends Shape{
	void draw() {
		System.out.println("Calling Square.draw()");
	}
	void erase() {
		System.out.println("Calling Square.erase()");
	}
}
class Triangle extends Shape{
	void draw() {
		System.out.println("Calling Triangle.draw()");
	}
	void erase() {
		System.out.println("Calling Triangle.erase()");
	}
}

public class Shapes {
	static void drawOneShape(Shape s) {
		s.draw();
	}
    static void drawShapes(Shape[] ss) {
    	for(int i=0;i<ss.length;i++) {
    		ss[i].draw();
    }
}
public static void main(String[] args) {
	Random rand=new Random();
	Shape[] s=new Shape[9];
	for(int i=0;i<s.length;i++) {
		switch(rand.nextInt(3)) {
		case 0:s[i]=new Circle();break;
		case 1:s[i]=new Square();break;
		case 2:s[i]=new Triangle();break;
		}
	}
	drawShapes(s);
 }
}

这段代码定义了一个名为Shapes的Java类,其中包含了一个main方法作为程序的入口点。在main方法中,首先创建了一个长度为9的Shape数组s,然后使用Random类生成随机数来初始化数组中的每个元素。根据随机数的值,数组中的每个元素被实例化为CircleSquareTriangle对象。

接下来,调用drawShapes方法,传入数组s作为参数。drawShapes方法遍历数组中的每个元素,并调用其draw方法来绘制形状。在这个例子中,draw方法会打印出相应的形状信息,例如"Calling Circle.draw()"、"Calling Square.draw()"或"Calling Triangle.draw()"。

当运行这段代码时,将会输出类似以下的结果(每次运行结果可能不同,因为随机数的生成是不确定的):

Calling Circle.draw()
Calling Square.draw()
Calling Triangle.draw()
Calling Circle.draw()
Calling Square.draw()
Calling Triangle.draw()
Calling Circle.draw()
Calling Square.draw()
Calling Triangle.draw()

package com.sxt;

public class Hello {
	public static void main(String[] args) {
		Human e=new Chinese("章","三");
		System.out.println("The first person:");
		e.showNameInNativeLanguage();
		System.out.println();
		
		Human a=new American("Goodman","Tom");
		System.out.println("The second person:");
		a.showNameInNativeLanguage();
		};
}
class Human{
	String surName;
	String givenName;
	String nationNal;
	public Human(String surName,String givenName) {
		this.surName=surName;
		this.givenName=givenName;
	}
	public void showNameInNativeLanguage() {
		System.out.println("Nationnality:"+nationNal);
	}
}
class Chinese extends Human{
	public Chinese(String surName,String givenName) {
		super(surName,givenName);
		nationNal="China";
	}
	public void showNameInNativeLanguage() {
		super.showNameInNativeLanguage();
		System.out.println("姓名:"+surName+" "+givenName);
	}
}
class American extends Human{
	public American(String surName,String givenName) {
		super(surName,givenName);
		nationNal="USA";
	}
	public void showNameInNativeLanguage() {
		super.showNameInNativeLanguage();
		System.out.println("Givenname and Surname:"+givenName+" "+surName);
	}
}

这段代码定义了一个名为Hello的Java类,其中包含了一个main方法作为程序的入口点。在main方法中,首先创建了一个Human类型的对象e,通过调用Chinese类的构造函数并传入参数"章"和"三"来实例化。然后输出"The first person:",并调用e对象的showNameInNativeLanguage方法来显示中国人的姓名。

接下来,创建了一个Human类型的对象a,通过调用American类的构造函数并传入参数"Goodman"和"Tom"来实例化。然后输出"The second person:",并调用a对象的showNameInNativeLanguage方法来显示美国人的姓名。

Human类中,定义了两个成员变量surNamegivenName,以及一个成员变量nationNalHuman类的构造函数接受两个参数,分别赋值给surNamegivenNameHuman类还定义了一个showNameInNativeLanguage方法,用于输出国籍信息。

Chinese类继承自Human类,在其构造函数中调用父类的构造函数,并将nationNal设置为"China"。Chinese类重写了showNameInNativeLanguage方法,先调用父类的showNameInNativeLanguage方法,再输出中国人的姓名。

American类也继承自Human类,在其构造函数中调用父类的构造函数,并将nationNal设置为"USA"。American类重写了showNameInNativeLanguage方法,先调用父类的showNameInNativeLanguage方法,再输出美国人的姓名。

当运行这段代码时,将会输出以下结果:

The first person:
Nationnality:China
姓名:章 三

The second person:
Nationnality:USA
Givenname and Surname:Tom Goodman