class MyObject {}
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile error.
}
}
class MyObject2 {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test2 {
public static void main(String[] args) throws CloneNotSupportedException {
MyObject2 obj = new MyObject2();
obj.clone(); // Compile OK.
}
}
package 1
class MyObject3 {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package 2
public class Test3 extends MyObject3 {
public static void main(String args[]) {
MyObject3 obj = new MyObject3();
obj.clone(); // Compile error.
Test3 tobj = new Test3();
tobj.clone();// Complie OK.
}
}
public class TStatic {
static int i;
public TStatic() {
i = 4;
}
public TStatic(int j) {
i = j;
}
public static void main(String args[]) {
System.out.println(TStatic.i);
TStatic t = new TStatic(5); // 声明对象引用,并实例化。此时i=5
System.out.println(t.i);
TStatic tt = new TStatic(); // 声明对象引用,并实例化。此时i=4
System.out.println(t.i);
System.out.println(tt.i);
System.out.println(t.i);
}
}
0 5 4 4 4
class ClassA {
int b;
public void ex1() {}
class ClassB {
void ex2() {
int i;
ClassA a = new ClassA();
i = a.b; // 这里通过对象引用访问成员变量b
a.ex1(); // 这里通过对象引用访问成员函数ex1
}
}
}
class ClassA {
static int b;
static void ex1() {}
}
class ClassB {
void ex2() {
int i;
i = ClassA.b; // 这里通过类名访问成员变量b
ClassA.ex1(); // 这里通过类名访问成员函数ex1
}
}
public class StaticCls {
public static void main(String[] args) {
OuterCls.InnerCls oi = new OuterCls.InnerCls();// 这之前不需要new一个OuterCls
}
}
class OuterCls {
public static class InnerCls {
InnerCls() {
System.out.println("InnerCls");
}
}
}
InnerCls
class Value {
static int c = 0;
Value() {
c = 15;
}
Value(int i) {
c = i;
}
static void inc() {
c++;
}
}
class Count {
public static void prt(String s) {
System.out.println(s);
}
Value v = new Value(10);
static Value v1, v2;
static {
prt("in the static block of calss Count v1.c=" + v1.c + " v2.c="
+ v2.c);
v1 = new Value(27);
prt("in the static block of calss Count v1.c=" + v1.c + " v2.c="
+ v2.c);
v2 = new Value();
prt("in the static block of calss Count v1.c=" + v1.c + " v2.c="
+ v2.c);
}
}
public class TStaticBlock {
public static void main(String[] args) {
Count ct = new Count();
Count.prt("in the main:");
Count.prt("ct.c=" + ct.v.c);
Count.prt("v1.c=" + Count.v1.c + " v2.c=" + Count.v2.c);
Count.v1.inc();
Count.prt("v1.c=" + Count.v1.c + " v2.c=" + Count.v2.c);
Count.prt("ct.c=" + ct.v.c);
}
}
in the static block of calss Count v1.c=0 v2.c=0 in the static block of calss Count v1.c=27 v2.c=27 in the static block of calss Count v1.c=15 v2.c=15 in the main: ct.c=10 v1.c=10 v2.c=10 v1.c=11 v2.c=11 ct.c=11
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有