A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jekyll 中级黑马   /  2015-9-21 18:54  /  207 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class review9_22 {
  2.         public static void main(String[] args) {
  3.                 //代码块
  4.                 //ARea area = new ARea();
  5.                 //继承动物类
  6.                 //Cat miao = new Cat("白",4);
  7.                 //miao.catchMouse();
  8.                 //继承类实例化过程
  9.                 //Zi z = new Zi();
  10.                 //重写手机类
  11.                 //Nokia nokia = new Nokia("Nokia",199);
  12.                 //nokia.call();
  13.                 /*
  14.                 * A:案例演示
  15.                 * 属性:姓名,年龄
  16.                 * 行为:吃饭
  17.                 * 老师有特有的方法:讲课
  18.                 * 学生有特有的方法:学习
  19.                 */
  20.                 /*Teacher t = new Teacher();
  21.                 Student s = new Student();
  22.                 t.teach();
  23.                 s.study();
  24.                 */
  25.         }
  26. }
  27. class Person {
  28.         private String name;
  29.         private int age;
  30.         Person() {}
  31.         void eat() {
  32.                 System.out.println("吃饭");
  33.         }
  34. }

  35. class Teacher extends Person {
  36.         void teach() {
  37.                 System.out.println("教课");
  38.         }
  39. }

  40. class Student extends Person {
  41.         void study() {
  42.                 System.out.println("学习");
  43.         }
  44. }


  45. class MobilePhone {
  46.         private String brand;
  47.         private int price;       

  48.         MobilePhone(String brand,int price) {
  49.                 this.brand = brand;
  50.                 this.price = price;
  51.         }

  52.         public String getBrand() {
  53.                 return this.brand;
  54.         }

  55.         public int getprice(){
  56.                 return this.price;
  57.         }

  58.         public void setprice(int price){
  59.                 this.price = price;
  60.         }

  61.         void call() {
  62.                 System.out.println("用"+this.brand+"打电话");
  63.         }

  64.         public void sendMessage(Student stu) {
  65.                 System.out.println("用"+this.brand+"发短信");
  66.         }

  67.         public void playGame() {
  68.                 System.out.println("用"+this.price+"的手机玩游戏");
  69.         }
  70. }

  71. class Nokia extends MobilePhone {
  72.         Nokia(String brand,int price) {
  73.                 super(brand,price);
  74.         }
  75.        
  76.         void call() {
  77.                 System.out.println("用"+getBrand()+"砸核桃");
  78.         }
  79. }


  80. class Fu {
  81.                         static {
  82.                                 System.out.println("静态代码块Fu");
  83.                         }
  84.        
  85.                         {
  86.                                 System.out.println("构造代码块Fu");
  87.                         }
  88.        
  89.                         public Fu() {
  90.                                 System.out.println("构造方法Fu");
  91.                         }
  92.                 }
  93.        
  94. class Zi extends Fu {
  95.                         static {
  96.                                 System.out.println("静态代码块Zi");
  97.                         }
  98.        
  99.                         {
  100.                                 System.out.println("构造代码块Zi");
  101.                         }
  102.        
  103.                         public Zi() {
  104.                                 System.out.println("构造方法Zi");
  105.                         }
  106.                 }


  107. /*
  108.         * 动物类,猫类,狗类
  109.         * 定义两个属性(颜色,腿的个数)两个功能(吃饭,睡觉)
  110. */

  111. class Animal {
  112.         String color;
  113.         int leg;
  114.        
  115.         Animal() {};

  116.         void eat() {
  117.                 System.out.println("吃饭");
  118.         }
  119.         void sleep() {
  120.                 System.out.println("睡觉");
  121.         }
  122. }

  123. class Cat extends Animal {
  124.         Cat(String color,int leg) {
  125.                 this.color = color;
  126.                 this.leg = leg;
  127.         }       
  128.         void catchMouse() {
  129.                 System.out.println(this.color+"喵捉老鼠");
  130.         }
  131. }

  132. class Dog extends Animal {
  133.         Dog(String color,int leg) {
  134.                 this.color = color;
  135.                 this.leg = leg;
  136.         }
  137.         void lookHome() {
  138.                 System.out.println(this.color+"看家");
  139.         }
  140. }

  141. class ARea {
  142.         static {
  143.                 System.out.println("静态代码块在方法区静态域加载,初始化类");
  144.         }

  145.         {
  146.                 System.out.println("构造代码块");
  147.         }

  148.         ARea() {
  149.                 System.out.println("构造方法");
  150.         }

  151. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马