Java继承和多态
- class Father{
- public int s=20;
- public void prints(){
- System.out.println(“Hello Father ” + s);
- }
- }
- public class HelloWorld extends Father {
- int s=30, t=40;
- public void prints(){
- System.out.println(“Hello World ” + s);
- }
- public static void main(String[] args) {
- Father tmp = new HelloWorld();
- tmp.prints();
- System.out.println(tmp.s);
- /*Scanner reader = new Scanner(System.in);
- while(reader.hasNextInt()){
- int tmps = reader.nextInt();
- System.out.printf(“%03d\n”,tmps);
- }*/
- }
- }
输出:
Hello World 30
20
注意方法是覆盖,类的变量是隐藏,不能实现覆盖。而多态只对覆盖关系才能体现出来。