java_字段声明

多字段继承,为避免混淆,simple name与qualified name的使用

package java20180129_1;

interface Frob {
    float v=2.0f;
}
class SuperTest{
    int v=3;
}
class Demo extends SuperTest implements Frob{
    public static void main(String[] args) {
        new Demo().printV();
    }
    
    /**
     * 多继承字段
     * 不能直接写v,要么写成super.v,要么写成Frob.v
     */
    void printV(){
//        System.out.println(v);
        System.out.println((Frob.v+super.v)/2);
    }
}
package java20180129_1;
interface Color{
    int RED=0,GREEN=1,BLUE=2;
}
interface TrafficLight{
    int RED=0,YELLOW=1,GREEN=2;
}
class Demo implements Color, TrafficLight {
    public static void main(String[] args) {
//        System.out.println(GREEN);
//        System.out.println(RED);
        
        System.out.println(TrafficLight.GREEN);
        System.out.println(Color.RED);
    }
}

下面的Red不会产生混淆

package java20180129_1;

interface Colorable {
    int RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff;
}

interface Paintable extends Colorable {
    int MATTE = 0, GLOSSY = 1;
}

class Point {
    int x, y;
}

class ColoredPoint extends Point implements Colorable {
}

class PaintedPoint extends ColoredPoint implements Paintable {
    int p = RED;
}
原文地址:https://www.cnblogs.com/createyuan/p/8378646.html