java类的一些特殊效果

上代码:

package com.wjy;

public class Test {
public static void main(String[] args){
Student s1=new Student(99);
}
}

class Student{
public static Student stu=new Student(22);//这里必须要static修饰,否则会出错。
public Student(int age){
System.out.println("The age is: "+age);
}
}

运行结果:

The age is: 22
The age is: 99

再看一个例子:

public class Test {
public static void main(String[] args) {
Activator activator = new Activator(23);
System.out.println("The age is " + activator.age);
}
}

class Activator {
private Activator plugin;
protected int age;

public Activator(int age) {
plugin = this;
this.age = age;
System.out.println("The age is " + age);
}

}

运行结果:

The age is 23
The age is 23

以上代码和下面的代码等价:

//*****************************************************************************

package com.wjy;

public class Test {
public static void main(String[] args) {
Activator activator = new Activator(23);


activator.showAge();
activator.getActivator().showAge();
}
}

class Activator {
private Activator plugin; //这里不需要用static修饰了。
protected int age;

public Activator(int age) {
this.age = age;
}

public Activator getActivator() {
plugin = this;
return plugin;
}

public void showAge() {
System.out.println("The age is " + age);
}
}

//****************************************************************************

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值