用java继承编写3个类,要求看下图
3个回答
展开全部
我将程序分为了两个文件,一个文件存放Student类、Freshman类(大一学生)和,Sophomore类(大二学生),另一个类存放主调程序类Exos11,具体代码如下所示,首先是学生类:
class Student
{
public String name;
Student(String name)
{
this.name = name;
}
public void learnMarxism()
{
System.out.println(this.name + " learn Marxism");
}
}
class Freshman extends Student
{
Freshman(String name)
{
super(name);
}
public void learnJavaSE()
{
System.out.println(this.name + " learn JavaSE");
}
}
class Sophomore extends Student
{
Sophomore(String name)
{
super(name);
}
public void learnJavaEE()
{
System.out.println(this.name + " learn JavaEE");
}
}
然后是主调程序类:
public class Exos11
{
public void printInfo()
{
Freshman fr = new Freshman("Jack");
Sophomore so = new Sophomore("Jane");
fr.learnMarxism();
so.learnMarxism();
fr.learnJavaSE();
so.learnJavaEE();
}
public static void main(String[] args)
{
Exos11 ex = new Exos11();
ex.printInfo();
}
}
以上就是我的答案,如有问题,欢迎追问。
更多追问追答
追问
你好,能不能做成一个文件的
追答
可以,只要将两个部分的代码整合就行了,具体如下:
class Student
{
public String name;
Student(String name)
{
this.name = name;
}
public void learnMarxism()
{
System.out.println(this.name + " learn Marxism");
}
}
class Freshman extends Student
{
Freshman(String name)
{
super(name);
}
public void learnJavaSE()
{
System.out.println(this.name + " learn JavaSE");
}
}
class Sophomore extends Student
{
Sophomore(String name)
{
super(name);
}
public void learnJavaEE()
{
System.out.println(this.name + " learn JavaEE");
}
}
class Exos11
{
public void printInfo()
{
Freshman fr = new Freshman("Jack");
Sophomore so = new Sophomore("Jane");
fr.learnMarxism();
so.learnMarxism();
fr.learnJavaSE();
so.learnJavaEE();
}
public static void main(String[] args)
{
Exos11 ex = new Exos11();
ex.printInfo();
}
}
运行的时候编译这个文件,然后运行java Exos11即可。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |