class Son{
int weight =100;
void f(){
weight = 200;
System.out.println(weight);
}
}
class Sunzi extends Son{
int weight=400;
/*void f(){
weight = 200;
System.out.println(weight);
}*/
public static void main(String[]args){
Sunzi sunzi= new Sunzi();
System.out.println(sunzi.weight);
sunzi.f();
System.out.println(sunzi.weight);//这里为什么不是输出200?f()方法不是继承下来了吗?
//能帮我具体解释一下成员变量与方法在子类重写与没有重新定义的区别吗?没有重新定义在子类
//与父类中是同一个吗?
}
}
而Son则只能访问Son的变量,不能访问Sunzi的变量
f()方法确实继承下来了
重写了方法的话就调用子类的方法了,如果没有重新定义则在父类中找,直到Object类
没有重新定义在子类与父类中是同一个
int weight =100;
void f(){
weight = 200;
System.out.println(weight);
}
}
class Sunzi extends Son{
int weight=400;
/*void f(){
weight = 200;
System.out.println(weight);
}*/
public static void main(String[]args){
Sunzi sunzi= new Sunzi();
Son son = new Son();
System.out.println(sunzi.weight);
sunzi.f();
System.out.println(son.weight);//输出的是100!也没改变到
//son.weight的值啊!?
System.out.println(sunzi.weight);
}
}
对于没有重新定义的成员还是有点模糊!能再详细说一下吗?thanks !
class Son{
int weight =100;
void f(){
weight = 200; //定义的是一个内部变量,完全可以不用weight,而用别的,以免让人与类变量weight产生误解
System.out.println(weight);
}
}
class Sunzi extends Son{ int weight=400;
public static void main(String[]args){ Sunzi sunzi= new Sunzi(); System.out.println(sunzi.weight); //调用Sunzi的weight=400,所以输出400;sunzi.f(); //调用继承下来的f()函数,f()里输出200,所以输出200;
System.out.println(sunzi.weight);//输出的是Sunzi的weight,,覆盖了Son的weight,所以是400;
}
}
class Son{ int weight =100; //weight 不是静态的,所以你声明的每个Son对象都会有一个weight,并且等于100;
void f(){
weight = 200;
System.out.println(weight);
}
}
class Sunzi extends Son{ int weight=400;
public static void main(String[]args){
Sunzi sunzi= new Sunzi();
Son son = new Son();
System.out.println(sunzi.weight);
sunzi.f();
System.out.println(son.weight);
/**
输出的当然是100了,因为你声明的son对象的weight=100;而你调用
sunzi.f(); 他调用的是父类的f()函数,但是这个父类的对象并不是你声明的son,
而是你一个未知的对象Son,(你声明的son, sunzi,在本质上没有什么关系,是独立的,
所有sunzi.f(),并不会改变son的类变量;*/ System.out.println(sunzi.weight);
}
}
/**
对我的想法我做一下分析,有两种解释
(1)
*/
class Son{
static int weight =100; //将weight声明为static,这样所有的类对象公用一个weight,任何一个类改变都会影响weight;
void f(){
weight=200;
System.out.println(weight);
}
}
class Sunzi extends Son{
int weight=400; //覆盖了Son的weight;
public static void main(String[]args){
Son son=new Son();
Sunzi sunzi= new Sunzi();
System.out.println(son.weight);//Song 的weight没有改变=100;
System.out.println(sunzi.weight); //调用Sunzi的weight=400,所以输出400;
sunzi.f();
/**调用继承下来的f()函数,输出200并改变父类的weight;但因为类Son的weight为static(公用的),所以以后任何Son对象的weight都改变为200;
*/
System.out.println(sunzi.weight);//输出的是Sunzi的weight,,覆盖了Son的weight,所以是400;
System.out.println(son.weight);//weight被改变输出200;
}
}/**
解释(2)使用多态
*/
class Son{
int weight =100; //声明为你的int类型,没有个对象都有一个weight,互不影响;
void f(){
weight=200;
System.out.println(weight);
}
}
class Sunzi extends Son{ int weight=400; //覆盖了Son的weight;public static void main(String[]args){ Sunzi sunzi= new Sunzi();
Son son=sunzi;//使用多态,将子类对象赋值给父类,使它们指向同一个对象;System.out.println(son.weight);//son指向他的类成员weight=100;
System.out.println(sunzi.weight); //调用Sunzi的weight=400,所以输出400;
sunzi.f(); //调用继承下来的f()函数,输出200,并改变父类的weight;
System.out.println(sunzi.weight);//输出的是Sunzi的weight,,覆盖了Son的weight,所以是400;
System.out.println(son.weight);//son和sunzi指向同一对象,sunzi改变了weight,所有son也改变了;
}
}
有不恰当之处请指出啊
使用多态里面代码有一点还不是很明白:
上面说的
Son son=sunzi;//使用多态,将子类对象赋值给父类,使它们指向同一个对象;
既然指向了同一对象,那他们的实例变量weight会不会是相等了!?那后面的两
个打印语句怎么不都输出200?对于继承,没有在子类重新定义的父类成员,操作产生的影响都是父类的,那对于子类来说它的继承意义主要在哪里?thanks again!
使用多态,将子类对象赋值给父类,使它们指向同一个对象;但是编译器将son看成了Son对象并不是Sunzi的对象,所以调用son.weight时候他调用的是Son类的weight;
关于继承,is-a关系可以判断是否将类设计为继承,并不是你想的想设计为继承就设计为继承,那样的话在你调用类方法时可能会产生很多想不到错误;继承主要是子类包括父类的所有继承下来的方法,并且声明了自己的不同与父类的处理方法,这样继承才有意义,但你的类太简单,只是覆盖了一个类变量,谈不上继承,通过你的类想说明白多态还是有困难的 例如:声明一个普通员工和经理的类
import java.util.*;public class ManagerTest
{
public static void main(String[] args)
{
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Employee[] staff = new Employee[3];
/** 将boss付给staff[0],使它们指向同一个对象,但编译器会将staff[0]看成
*Employee对象,他不会去调用子类Manager类重新声明的方法;这个对理解你的程序很关键
*在你的程序中所以使用多态son.weight将调用son自己的weight而不是sunzi的weight;
*例如调用//staff[0].setBonus(5000);将会编译不通过;
*/
staff[0] = boss;
boss.setBonus(5000);//boss属于Mamager,所以可以
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15); // print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",salary=" + e.getSalary());
}
}class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public Date getHireDay()
{
return hireDay;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} private String name;
private double salary;
private Date hireDay;
}class Manager extends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
bonus = 0;
}
/**
*这些都是继承下来的方法,
*使用多态时都可以调用,它们和父类方法一样
*所有相当与调用父类方法
public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public Date getHireDay()
{
return hireDay;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
*/
public double getSalary()//重新声明的自己的方法,父类对象不能调用
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double b)//重新声明的自己的方法,父类对象不能调用
{
bonus = b;
} private double bonus;
}
不恰当之处请指出,建议你找本基础知识的书籍多看看继承那一块