java 继承题目
2.编写一个程序,用于创建一个名为Employee的父类和两个名为Manager和Director的子类。Employee类包含3个属性和一个方法,属性为name、bas...
2.编写一个程序,用于创建一个名为Employee的父类和两个名为
Manager和Director的子类。Employee类包含3个属性和一个方法,属
性为name、basic(基本工资)和address,方法名为show(),用于显示这些属性的值。Manager类有一个称为department(部门)的附加属性。Director类有一个称为transportAllowance(交通津贴)的附加属性。创建Manager和Director类的对象,并显示其详细信息。
这里老师还没教 不知道怎么做了 请前辈们帮帮忙写一个吧 最好附带说明 谢谢了 展开
Manager和Director的子类。Employee类包含3个属性和一个方法,属
性为name、basic(基本工资)和address,方法名为show(),用于显示这些属性的值。Manager类有一个称为department(部门)的附加属性。Director类有一个称为transportAllowance(交通津贴)的附加属性。创建Manager和Director类的对象,并显示其详细信息。
这里老师还没教 不知道怎么做了 请前辈们帮帮忙写一个吧 最好附带说明 谢谢了 展开
4个回答
展开全部
//继承
public class test {
public static void main (String args[]){
Manager m=new Manager ("小明",5000,"北京","java工程师"); //创建一个经理对象
System.out.println("经理的信息:");
m.show(); //输出信息
Director d =new Director ("小娃",6000,"广州",500); //创建一个director对象
System.out.println("\n Director 的信息:"); //调用函数输出信息
d.show();
}
}
class Employee{ //创建Employee类
String name ;
float basic;
String address;
Employee (String nam,float bas,String add){ //初始化
name=nam;
basic=bas;
address=add;
}
public void show (){
System.out.println("名字:"+this.name);
System.out.println("基本工资::"+this.basic);
System.out.println("地址:"+this.address);
}
}
class Manager extends Employee{ //Manager类继承了父类的属性和方法
String department;
Manager (String nam, float bas, String add,String dep) {
super(nam, bas, add); //使用super关键字
department=dep;
}
public void show() {
super.show();
System.out.println("部门:"+department);
}
}
class Director extends Employee{ //同上一个类一样的
double transportAllowance;
Director(String nam, float bas, String add,double tra) {
super(nam, bas, add);
transportAllowance=tra;
}
public void show() {
super.show();
System.out.println("交通补贴:"+transportAllowance);
}
}
public class test {
public static void main (String args[]){
Manager m=new Manager ("小明",5000,"北京","java工程师"); //创建一个经理对象
System.out.println("经理的信息:");
m.show(); //输出信息
Director d =new Director ("小娃",6000,"广州",500); //创建一个director对象
System.out.println("\n Director 的信息:"); //调用函数输出信息
d.show();
}
}
class Employee{ //创建Employee类
String name ;
float basic;
String address;
Employee (String nam,float bas,String add){ //初始化
name=nam;
basic=bas;
address=add;
}
public void show (){
System.out.println("名字:"+this.name);
System.out.println("基本工资::"+this.basic);
System.out.println("地址:"+this.address);
}
}
class Manager extends Employee{ //Manager类继承了父类的属性和方法
String department;
Manager (String nam, float bas, String add,String dep) {
super(nam, bas, add); //使用super关键字
department=dep;
}
public void show() {
super.show();
System.out.println("部门:"+department);
}
}
class Director extends Employee{ //同上一个类一样的
double transportAllowance;
Director(String nam, float bas, String add,double tra) {
super(nam, bas, add);
transportAllowance=tra;
}
public void show() {
super.show();
System.out.println("交通补贴:"+transportAllowance);
}
}
展开全部
package com.jin.common.util;
import java.lang.reflect.Field;
public class Employee {
public String name;
public String address;
public int basic;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getBasic() {
return basic;
}
public void setBasic(int basic) {
this.basic = basic;
}
public void show(Class a){
Field propertys[]=a.getDeclaredFields();
try {
for(Field property:propertys){
System.out.println("属性名:"+property.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name:"+name);
System.out.println("address:"+address);
System.out.println("basic:"+basic);
}
}
class Manager extends Employee{
private String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Manager(){
this.show(this.getClass());
}
@Override
public void show(Class a) {
this.show(a);
}
}
class Director extends Employee{
public Director(){
this.show(this.getClass());
}
@Override
public void show(Class a ) {
this.show(a);
}
}
,仅作为参考。
import java.lang.reflect.Field;
public class Employee {
public String name;
public String address;
public int basic;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getBasic() {
return basic;
}
public void setBasic(int basic) {
this.basic = basic;
}
public void show(Class a){
Field propertys[]=a.getDeclaredFields();
try {
for(Field property:propertys){
System.out.println("属性名:"+property.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name:"+name);
System.out.println("address:"+address);
System.out.println("basic:"+basic);
}
}
class Manager extends Employee{
private String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Manager(){
this.show(this.getClass());
}
@Override
public void show(Class a) {
this.show(a);
}
}
class Director extends Employee{
public Director(){
this.show(this.getClass());
}
@Override
public void show(Class a ) {
this.show(a);
}
}
,仅作为参考。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。
java多态存在的三个必要条件:
1.需要有继承关系的存在
2.需要有方法的重写
3.需要有父类的引用指向子类对象
希望对你有帮助。
第一题应该选D,第二题选C,D。
第一题属于多态,methodB()方法属于子类,父类没有重写子类的方法
第二题属于继承,子类可以继承父类的方法
java多态存在的三个必要条件:
1.需要有继承关系的存在
2.需要有方法的重写
3.需要有父类的引用指向子类对象
希望对你有帮助。
第一题应该选D,第二题选C,D。
第一题属于多态,methodB()方法属于子类,父类没有重写子类的方法
第二题属于继承,子类可以继承父类的方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package inherit;
public class Employee {
//constructor
public Employee(String name, double basic, String address) {
super();
this.name = name;
this.basic = basic;
this.address = address;
}
public void show()
{
System.out.println("Name: " + name + "\nBasic: " + basic + "\nAddress: " + address);
}
//getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasic() {
return basic;
}
public void setBasic(double basic) {
this.basic = basic;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private String name;
private double basic;
private String address;
}
//////////////////////
package inherit;
public class Manager extends Employee{
//constructor
public Manager(String name, double d, String address, String department) {
super(name, d, address);//call parent's show function
this.department = department;
}
//getter and setter
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public void show()
{
super.show();
System.out.println("Department: " + department);
}
private String department;
}
//////////////////////////
package inherit;
public class Director extends Employee{
//constructor
public Director(String name, double d, String address, double e) {
super(name, d, address);
this.transportAllowance = e;
}
public void show()
{
super.show();//call parent's show function
System.out.println("TransportAllowance: " + transportAllowance);
}
//getter and setter
public double getTransportAllowance() {
return transportAllowance;
}
public void setTransportAllowance(double transportAllowance) {
this.transportAllowance = transportAllowance;
}
private double transportAllowance;
}
/////////////////////////////////
package inherit;
public class Main {
public static void main(String args[])
{
Manager m = new Manager("hq", 898.12, "DUT", "CS");
Director d = new Director("hq", 898.12, "DUT", 123.234);
m.show();
d.show();
}
}
public class Employee {
//constructor
public Employee(String name, double basic, String address) {
super();
this.name = name;
this.basic = basic;
this.address = address;
}
public void show()
{
System.out.println("Name: " + name + "\nBasic: " + basic + "\nAddress: " + address);
}
//getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasic() {
return basic;
}
public void setBasic(double basic) {
this.basic = basic;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private String name;
private double basic;
private String address;
}
//////////////////////
package inherit;
public class Manager extends Employee{
//constructor
public Manager(String name, double d, String address, String department) {
super(name, d, address);//call parent's show function
this.department = department;
}
//getter and setter
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public void show()
{
super.show();
System.out.println("Department: " + department);
}
private String department;
}
//////////////////////////
package inherit;
public class Director extends Employee{
//constructor
public Director(String name, double d, String address, double e) {
super(name, d, address);
this.transportAllowance = e;
}
public void show()
{
super.show();//call parent's show function
System.out.println("TransportAllowance: " + transportAllowance);
}
//getter and setter
public double getTransportAllowance() {
return transportAllowance;
}
public void setTransportAllowance(double transportAllowance) {
this.transportAllowance = transportAllowance;
}
private double transportAllowance;
}
/////////////////////////////////
package inherit;
public class Main {
public static void main(String args[])
{
Manager m = new Manager("hq", 898.12, "DUT", "CS");
Director d = new Director("hq", 898.12, "DUT", 123.234);
m.show();
d.show();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询