public class person
{ int no;
Stirng name;
int age;
person(int age)
{
this.age=age;
}
person(String name,int no)
{
this.name=name;
this.no=no;
}
void show()
{
System.out.println("name="+name+","+"no="+no);
} void print() { System.out.println("age="+age); }}public class student extends person
{ int ;
person(String name,int no,int ) { super(name);
super(no);
this.=;
}
void show()
{ super.show();
System.out.println("="+);
} student(int aga)
{ super(aga); }
public static void main()
{ int x,y,z;
String m;
student r=new student(m,x,y);
r.show();
r.student(z);
super.print();
}}
其中的方法覆盖和重载总用起来不清楚,请大家解释一下。
{ int no;
Stirng name;
int age;
person(int age)
{
this.age=age;
}
person(String name,int no)
{
this.name=name;
this.no=no;
}
void show()
{
System.out.println("name="+name+","+"no="+no);
} void print() { System.out.println("age="+age); }}public class student extends person
{ int ;
person(String name,int no,int ) { super(name);
super(no);
this.=;
}
void show()
{ super.show();
System.out.println("="+);
} student(int aga)
{ super(aga); }
public static void main()
{ int x,y,z;
String m;
student r=new student(m,x,y);
r.show();
r.student(z);
super.print();
}}
其中的方法覆盖和重载总用起来不清楚,请大家解释一下。
楼主【zhangbin55661】截止到2008-07-15 08:54:03的历史汇总数据(不包括此帖):
发帖的总数量:9 发帖的总分数:180 每贴平均分数:20
回帖的总数量:5 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:7 结贴的总分数:140
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:40
结贴的百分比:77.78 % 结分的百分比:77.78 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油
因为student的SHOW覆盖了person的SHOW,你NEW 的是STUDENT就调STUDENT的SHOW,NEW PERSON就调PERSON
如果NEW 的是STUDENT的而STUDENT里没有SHOW方法才调用PERSON的SHOW方法
class Person { int no;
String name;
int age; public Person(int age) {
this.age = age;
} public Person(String name, int no) {
this.name = name;
this.no = no;
} public void show() {
System.out.println("name=" + name + "," + "no=" + no);
} public void print() {
System.out.println("age=" + age); }
}public class Student extends Person { public int ; public Student(String name, int no, int ) {
super(name, no);
this. = ;
} public Student(int aga) {
super(aga);
} public void show() {
super.show();
System.out.println("=" + );
} public static void main(String args[]) {
int no = 15;
int = 1;
String m = "zhangsan"; Student r = new Student(m, no, );
r.show();
}
} 先把代码给你整理了一下,错误太多了。
super(no);
不能这样搞吧!
这个写的像绕口令一样,我觉得可以直观的点说。子类对象除开不能直接调用父类的构造方法,其他的方法和属性都可以直接调取。如果是父类声明的对象,是无法直接调取子类的方法和属性的。
重写---------肯定是在两个类中,并且一定会是 继承 或者 是实现接口关系。 然后 重写需要注意的事项: 具备有相同的名称和类型,对象调用参数也要相同。重载---------肯定是在一个类里面,方法名字相同,但是参数项里面的参数数量一定不相同。另外重写最好的例子 我觉得就是抽象方法的实现 和 interface中的方法给接入类实现,全部都是重写。另外一点请教其他人:我觉得他上面这段代码如果是要这样写的话,我个人认为父类 声明为 abstract 好些,没出现抽象的类,平凡使用 super();我觉得没有必要。想要清楚了解 重载和重写 其实分别做两个简单的数值传递比较就可以了。 以上代码我觉得很容易把自己都看晕的!
{
public String CustomerID;
public String CustomerName;
public String CustomerPhone;
public Customer()
{
CustomerID="ASD";
CustomerName="Hanny";
CustomerPhone="0731-6214205";
}
public void display()
{
System.out.println("CustomerID="+CustomerID);
System.out.println("CustomerName="+CustomerName);
System.out.println("CustomerPhone="+CustomerPhone);
}
}
class VIPCustomer extends Customer
{
int jifen=123;
public VIPCustomer(int jifen)
{
this.jifen = jifen;
}
public void diplays()
{
super.display();
System.out.println("jifen="+jifen);
}
public static void main(String args[])
{
VIPCustomer obj = new VIPCustomer();
obj.diplays();
}
}