题目描述
编写Student类(继承Person类),
包含构造方法:
public Student()
public Student (String dep, String name,int age)
包含成员变量:
private String department;
Main类及Person类已经编写好。
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner r = new Scanner(System.in);
String dep;
String name;
int age;
dep = r.next();
name = r.next();
if( !r.hasNextInt() )
return;
age = r.nextInt();
if( age < 0 )
return;
Person stu1=new Student(dep,name,age);
Person stu2=new Student();
stu1.show();
stu2.show();
}
}
class Person
{
protected String name;
private int age;
public Person()
{
this("anonymous",0);
System.out.println("called the non-argument constructor of Person class");
}
public Person(String name, int age)
{
System.out
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)