求助大神如何用Java写出这两个题目的代码?
答案:1 悬赏:20 手机版
解决时间 2021-03-01 02:31
- 提问者网友:寂寞撕碎了回忆
- 2021-02-28 13:46
求助大神如何用Java写出这两个题目的代码?
最佳答案
- 五星知识达人网友:长青诗
- 2021-02-28 14:23
第一题
package baidu;
public class Student {
protected String name;
protected int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void show(){
System.out.println("name = " + name);
System.out.println("age = " + age);
}
}
---------------------------------
package baidu;
public class Undergraduate extends Student {
private String degree;
public Undergraduate(String name, int age, String degree) {
super(name, age);
this.degree = degree;
}
public void show(){
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("degree = " + degree);
}
}
------------------------
package baidu;
public class TestRun {
public static void main(String[] args) {
Student s = new Student("singi",12);
s.show();
Undergraduate ug = new Undergraduate("lily",23,"本科");
ug.show();
}
}
输出:
name = singi
age = 12
name = lily
age = 23
degree = 本科
第二题:
package baidu;
public class Man {
String name;
public Man(String name) {
this.name = name;
}
public void business(){
System.out.println("在方法内输出谈生意");
}
}
----------------
package baidu;
public class SuperMan extends Man {
String name;
public SuperMan(String name, String name1) {
super(name);
this.name = name1;
}
@Override
public void business() {
System.out.println("在方法内输出谈几个亿的大单子");
}
public void fly(){
System.out.println("在方法内输出超人不会飞");
}
}
------------------
package baidu;
public class TestMan {
public static void main(String[] args) {
Man m = new Man("singi");
System.out.println(m.name);
m.business();
SuperMan sm = new SuperMan("lily","xiaoming");
System.out.println(sm.name);
sm.business();
sm.fly();
}
}
输出:
singi
在方法内输出谈生意
xiaoming
在方法内输出谈几个亿的大单子
在方法内输出超人不会飞
package baidu;
public class Student {
protected String name;
protected int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void show(){
System.out.println("name = " + name);
System.out.println("age = " + age);
}
}
---------------------------------
package baidu;
public class Undergraduate extends Student {
private String degree;
public Undergraduate(String name, int age, String degree) {
super(name, age);
this.degree = degree;
}
public void show(){
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("degree = " + degree);
}
}
------------------------
package baidu;
public class TestRun {
public static void main(String[] args) {
Student s = new Student("singi",12);
s.show();
Undergraduate ug = new Undergraduate("lily",23,"本科");
ug.show();
}
}
输出:
name = singi
age = 12
name = lily
age = 23
degree = 本科
第二题:
package baidu;
public class Man {
String name;
public Man(String name) {
this.name = name;
}
public void business(){
System.out.println("在方法内输出谈生意");
}
}
----------------
package baidu;
public class SuperMan extends Man {
String name;
public SuperMan(String name, String name1) {
super(name);
this.name = name1;
}
@Override
public void business() {
System.out.println("在方法内输出谈几个亿的大单子");
}
public void fly(){
System.out.println("在方法内输出超人不会飞");
}
}
------------------
package baidu;
public class TestMan {
public static void main(String[] args) {
Man m = new Man("singi");
System.out.println(m.name);
m.business();
SuperMan sm = new SuperMan("lily","xiaoming");
System.out.println(sm.name);
sm.business();
sm.fly();
}
}
输出:
singi
在方法内输出谈生意
xiaoming
在方法内输出谈几个亿的大单子
在方法内输出超人不会飞
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯