public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) { //为什么加上final会没有错呢?怎么样才能有错呢
o.i++;
}
}
class Other {
public int i;
}
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) { //为什么加上final会没有错呢?怎么样才能有错呢
o.i++;
}
}
class Other {
public int i;
}
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
1).修饰class ==>该类不可继承
final class A{}
class B extends A{} //错误
2).修饰方法 ==>该方法不能覆写(overwritten)
class A{
final void fun(){}
} class B extends A{
final void fun(){} //error
}3).修饰变量
a). 基本类型 ==>值不能改变
final int x = 123;
x++; //error
b). 对象类型 ==>引用不能改变
final MyClass cls = new MyClass();
cls.xyzField++; //OK
cls = new MyClass(); //error
关于第二个,c继承b,实现接口X不会被覆盖吗