public static void main(String[] args) {
Son son = new Son();
son.show();
}作者: 柴乔军 时间: 2013-1-19 17:55
class Father
{
int num1 = 5;
}
class Son extends Father
{
int num1 = 20;
int num2 = 10;
public void show()
{
int num1 = 30;
System.out.println("num1:" + num1);
System.out.println("num2:" + num2);
// 局部范围内有的变量,如果我想使用成员变量,怎么办?this
System.out.println("this num1:" + this.num1);
// 就想子类中访问父类中和子类同名的变量super
System.out.println("father num1:" + super.num1);
}
}
public class Test2 {
public static void main(String[] args) {
Son s = new Son();
s.show();
}
}
复制代码
\
结果:
num1:30
num2:10
this num1:20
father num1:5作者: 罗广伟 时间: 2013-1-19 18:10
//在程序最后加上一下代码
class Test //定义一个Test类,用于存放主函数
{
public static void main(String[] args)//主函数
{
Son s= new Son();//创建对象
s.show();//调用show方法
}
}