当前位置: 代码迷 >> Java相关 >> 多态问题
  详细解决方案

多态问题

热度:363   发布时间:2006-11-11 12:22:43.0
多态问题

public abstract class Person {

public abstract String getDescription();
public String getName(){return null;}

}

---------------------------------------------------------------------------------

public class Employee extends Person
{
public Employee(String n,double s)
{

name=n; //error
salary=s;

}
public String getName()
{
return name; //error
}
public String getDescription()
{
return String.format("an employee with a salary of $%.2f",salary);
}
private double salary;
private name; //error
}

--------------------------------------------------------------------------------------------

public class Student extends Person
{
public Student(String n,String m)
{

name=n; //error
major=m;
}
public String getName()
{
return name; //error
}
public String getDescription()
{
return "a student majoring in"+major;
}
private String major;
private name; //error

}

----------------------------------------------------------------------------------------------

public class PersonTest {
public static void main(String[] args) {
Person [] people=new Person[2];
people[0]=new Employee("Harry Hacker",50000);
people[1]=new Student("Maria Morris","computer science");
for(Person p:people)
System.out.println(p.getName()+"."+p.getDescription());
}
}

为什么说我name错啊
我想实现多态!

[此贴子已经被作者于2006-11-11 12:41:37编辑过]

搜索更多相关的解决方案: 多态  

----------------解决方案--------------------------------------------------------

怎么没有人回帖啊


----------------解决方案--------------------------------------------------------
随便给点建议啊 看法都可以啊
----------------解决方案--------------------------------------------------------
你在超 类里面根本就没有定义name

----------------解决方案--------------------------------------------------------
个人意见!

abstract class Person
{
public String name;
public abstract String getDescription();
public String getName(){return null;}

}


class Employee extends Person
{
public Employee(String n,double s)
{

name=n; //error
salary=s;

}
public String getName()
{
return name; //error
}
public String getDescription()
{
return String.format("an employee with a salary of $%.2f",salary);
}
private double salary;
private String sname; //error
}


class Student extends Person
{
public Student(String n,String m)
{

name=n; //error
major=m;
}
public String getName()
{
return name; //error
}
public String getDescription()
{
return "a student majoring in"+major;
}
private String major;
//error

}



public class test {

public static void main(String[] args) {
Person [] people=new Person[2];
people[0]=new Employee("Harry Hacker",50000);
people[1]=new Student("Maria Morris","computer science");
for(Person p:people)
System.out.println(p.getName()+"."+p.getDescription());
}
}
}


----------------解决方案--------------------------------------------------------

类不应该这样设计的


----------------解决方案--------------------------------------------------------
方法重载 必须参数不同
----------------解决方案--------------------------------------------------------

public class PersonTest {
public static void main(String[] args) {
Employee e=new Employee("chen");
Student s=new Student("sheng");
System.out.print(e.getName());
System.out.print(s.getName());
}
}


我把 PersonTest 改成这样 还是不行


----------------解决方案--------------------------------------------------------
我想知道 抽象方法 和 多态有什么不同
抽象方法要在子类中重写而多态不也正是重写父类的方法吗
抽象又有什么意义呢
----------------解决方案--------------------------------------------------------
呵呵,怎么讲呢!
在抽象中是定义一种方式,而不是去具体怎么做!
比如,我们都是人,而人是个抽象的概念!人就有人的特征,具体到什么人就有不同的性格等等!
多态中父类有自己的实现方式,而子类终就不一定一样来实现!
就好比,儿子继承了父亲的一些特征,可能有不一致的地方!

----------------解决方案--------------------------------------------------------
  相关解决方案