⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testanimal.java

📁 java课件
💻 JAVA
字号:
/*
 * TestAnimal.java
 *
 * Created on 2007年10月3日, 上午2:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author admin
 */
public class TestAnimal {
  public static void main(String[] args) {
    Animal x = new Tiger();
    
    System.out.println("引用变量x的类: Animal, x引用的对象的类: Tiger");
    //访问news
    System.out.println("(1) x.news is " + x.news);
    System.out.println("(2) ((Tiger)x).news is " + ((Tiger)x).news);
    
    //调用静态方法
    System.out.println("(3) x.smile() is " + x.smile());
    System.out.println("(4) ((Tiger)x).smile() is " + ((Tiger)x).smile());
    
    //调用实例方法getNews
    System.out.println("(5) x.getNews() is " + x.getNews());
    System.out.println("(6) x.getMessage() is " + x.getMessage());
  }
}

class Animal{
  public String news = "Animal's news";
  public String message = "Animal's message";
  
  public static String smile(){
    return "smile from Animal";
  }
  
  public String getNews(){
    return news;
  }
  
  public String getMessage(){
    return message;
  }
}

class Tiger extends Animal {
  public String news = "Tiger's news";
  public String message = "Tiger's message";
  
  public static String smile(){
    return "smile from Tiger";
  }
  
  /**覆盖父类的getNews方法*/
  public String getNews(){
    return news;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -