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
类生成随机数来初始化数组中的每个元素。根据随机数的值,数组中的每个元素被实例化为Circle
、Square
或Triangle
对象。
接下来,调用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
类中,定义了两个成员变量surName
和givenName
,以及一个成员变量nationNal
。Human
类的构造函数接受两个参数,分别赋值给surName
和givenName
。Human
类还定义了一个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