方法重写 package text2; public class text2 { public static void main(){ //把子类复制给父类 对的 People p; Student s = new Student(); p = s; //把父类复制给子类 错误的 /*People p1 = new People(); Student s1; s1 = p1;*/ //可以把p1强制转化为student类 People p1 = new People(); Student s1; s1 = (Student)p1; } } class People{ int age; public void eat(){ System.out.println("吃吃吃...."); } } class Student extends People{ public void eat(){ System.out.println("学学学...."); } }
package text2; public class text4 { public static void main(String[] args){ T t = new T(); System.out.println(t.i) ; } } class T{ //private int i = 10; //会报错 protected int i = 10; //不会报错 //public int i = 10; //不会报错 }
静态变量 package text2; public class text5 { public static void main(String[] args){ System.out.println(Chinese.country); Chinese c1 = new Chinese(); Chinese c2 = new Chinese(); System.out.println(c1.country); c2.set("aaa"); System.out.println(c1.country); } } class Chinese{ static String country = "China"; String name; int age; void sing(){ System.out.println(country); } void set(String s){ country = s; } }
final:不可以被修改 package text2; public class text7 { final int a = 0 ; public static void main(String[] args){ new text7(); } public text7(){ //a = 1; //a = 9; System.out.println(a); } }
接口:只有函数声明,不可以被实例化,interface定义接口,implements接口实现 public class text9 implements{ //展示使用方法 } interface F{ public void fun1(); public void fun2(); public void fun3(); }