class 的使用

我们建完的class,并编进了相关变量和方法,但是,类只是一个模板,如果需要使用还需要对其进行实例化,也就是生成相应的对象。

而对象的使用则包括引用对象的成员变量和方法 ,通过“.”运算符可以实现对实例变量的访问和对实例方法的调用。

class Superclass
{
int x=3;
// Superclass()
// {
//
// x=3;
// System.out.println("in superclass:x=" + x);
// }
void dosomething()
{
System.out.println("in Superclass.dosomething");
}
}
class subclass extends Superclass
{
int x=5;
// public subclass() {
// // TODO Auto-generated constructor stub
// super();
// x=5;
// System.out.println("in subbclass:x="+x);
// }
@Override
void dosomething() {
// TODO Auto-generated method stub
super.dosomething();
System.out.println("In subclass.dosomething()");
System.out.println("super.x="+ super.x +" sbu.x="+x);
}

}
public class superORthis {
public static void main(String[] args) {
subclass subc=new subclass();
subc.dosomething();
}

原文地址:https://www.cnblogs.com/zhanggongchang/p/3910026.html