class father
{
int a=1,6=2;
void show()
{
System.out.println(a+" "+b);
}
}
class som extends father
{
int b=3,c=4;
void show()
{
system.out.println(a+" "+b+" "+c); }
}
class jex6_24
{
public static void main(string[] args)
{
son S =new son();
S.show();
farther Fb=S;
Fb.show();
System.out.println(Fb.a+""+Fb.b);
if(Fb instanceof son)
S=(son)Fb;
System.out.println(S.a+""+S.b+""+S.c);
}
}
{
int a=1,6=2;
void show()
{
System.out.println(a+" "+b);
}
}
class som extends father
{
int b=3,c=4;
void show()
{
system.out.println(a+" "+b+" "+c); }
}
class jex6_24
{
public static void main(string[] args)
{
son S =new son();
S.show();
farther Fb=S;
Fb.show();
System.out.println(Fb.a+""+Fb.b);
if(Fb instanceof son)
S=(son)Fb;
System.out.println(S.a+""+S.b+""+S.c);
}
}
son S =new son(); //代码错误
int a=1,6=2;
子类对象引用问题
这个怎么了??
1. 在class father 中6=2,这里可能是b=2;
2. class som extends father 这里类的名字应该是son,你把n写成m。
3. 还是在son类中,system.out.println(a+" "+b+" "+c); //这里的system的S应该是大写的--〉System
4.jex6_24中public static void main(string[] args) {//这里的string[]中的S应该是大写的--〉String
5.jex6_24中farther Fb = S;//这里应该是把farther--〉father .多了一个r。
{
int a=1,b=2;
void show()
{
System.out.println(a+" "+b);
}
}
class son extends farther
{
int b=3,c=4;
void show()
{
System.out.println(a+" "+b+" "+c); }
}
public class jex6_24
{
public static void main(String[] args)
{
son S =new son();
S.show();
farther Fb=S;
Fb.show();
System.out.println(Fb.a+" "+Fb.b);
//if(Fb instanceof son)
S=(son)Fb;
System.out.println(S.a+" "+S.b+" "+S.c);
}
}
输出:
1 3 4
1 3 4
1 2
1 3 4farther Fb=S; 形成多态,show方法只调用子类重写的方法。但是属性不重写。
{
int a=1,b=2;
void show()
{
System.out.println(a+" "+b);
}
}
class Son extends Father
{
int b=3,c=4;
void show()
{
System.out.println(a+" "+b+" "+c); }
}
class Test1
{
public static void main(String[] args)
{
Son S =new Son();
S.show();
Father Fb=S;
Fb.show();
System.out.println(Fb.a+""+Fb.b);
if(Fb instanceof Father)
S=(Son)Fb;
System.out.println(S.a+""+S.b+""+S.c);
}
}
楼上说的很对,属性是不会被重写的,子类会把父类中的方法覆盖住
我汗~~~估计楼主是想当高手,用记事本写代码~~~~找个IDE用用吧~~~
class Father {
int a = 1;
int b = 2; void show() {
System.out.println(a + " " + b);
}
}class Son extends Father {
int b = 3, c = 4; void show() {
System.out.println(a + " " + b + " " + c); }
}public class jex6_24 {
public static void main(String[] args) {
Son S = new Son();
S.show();
Father Fb = S;
Fb.show();
System.out.println(Fb.a + "" + Fb.b);
if (Fb instanceof Son)
S = (Son) Fb;
System.out.println(S.a + "" + S.b + "" + S.c);
}
}