JAVA Inherit

USERS – A

package test;

 

public class Car {

      public void sartUp(Gas g) {

            if (g.isEnough()) {

                  System.out.println("======Gas is enough======");

            } else {

                  System.out.println("======Gas isn't enough======");

            }

      }

}

package test;

 

public class Gas {

      private int cubage;

 

      public Gas(int cubage) {

            this.cubage = cubage;

      }

 

      public boolean isEnough() {

            if (cubage > 80) {

                  return true;

            } else {

                  return false;

            }

      }

}

package test;

 

public class Client {

 

      /**

       * @param args[0] is cubage of gas

       */

      public static void main(String[] args) {

            String cubage = args.length < 1 ? null : args[0];

            Gas gas = new Gas(Integer.valueOf(cubage));

            Car car = new Car();

            car.sartUp(gas);

      }

 

}

 

HAS - A

package test;

 

public class Employee {

      public Info getInfo() {

            return new Info();

      }

}

package test;

 

public class Info {

      private String name = "Marry";

      private String sex = "female";

      private int age = 26;

      private String address = "TianFu";

 

      public String getName() {

            return name;

      }

 

      public void setName(String name) {

            this.name = name;

      }

 

      public String getSex() {

            return sex;

      }

 

      public void setSex(String sex) {

            this.sex = sex;

      }

 

      public int getAge() {

            return age;

      }

 

      public void setAge(int age) {

            this.age = age;

      }

 

      public String getAddress() {

            return address;

      }

 

      public void setAddress(String address) {

            this.address = address;

      }

 

}

package test;

 

public class Client {

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Employee employee = new Employee();

            System.out.println("Name: " + employee.getInfo().getName());

            System.out.println("Sex: " + employee.getInfo().getSex());

            System.out.println("Age: " + employee.getInfo().getAge());

            System.out.println("Address: " + employee.getInfo().getAddress());

      }

 

}

 

IS – A

IS – A is base on inheritance

package test;

 

public class Fruit {

      final static private String fruitGrowMsg = "fruit growing";

      final static private String fruitHarvestMsg = "fruit harvesting";

 

      public void grow() {

            System.out.println(fruitGrowMsg);

      }

 

      public void harvest() {

            System.out.println(fruitHarvestMsg);

      }

}

package test;

 

public class Apple extends Fruit {

      final static private String appleHarvestMsg = "apple harvesting";

      private String type = "green";

 

      @Override

      public void harvest() {

            System.out.println(appleHarvestMsg);

      }

 

      public String getType() {

            return type;

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Apple apple = new Apple();

            System.out.println(apple.getType());

            apple.grow();

            apple.harvest();

      }

 

}

 

super & this in inheritance

package test;

 

public class Father {

      public Father() {

            super();

            System.out.println("invoke no parameter father structure");

      }

 

      public Father(String relation) {

            // super();

            System.out.println("invoke parameter (" + relation

                        + ") father structure");

      }

 

      public void getCharactor() {

            System.out.println("father is very moderate");

      }

}

package test;

 

public class Son extends Father {

 

      public Son() {

            super();

            System.out.println("invoke no parameter son structure");

      }

 

      public Son(String relation) {

            // super(relation);

            System.out.println("invoke parameter (" + relation + ") son structure");

      }

 

      @Override

      public void getCharactor() {

            System.out.println("son is very irritable");

      }

 

      public static void printSign() {

            System.out.println("====================");

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Father father = new Father();

            Son.printSign();

            Father fatherRel = new Father("father");

            Son.printSign();

            father.getCharactor();

            Son.printSign();

            Son son = new Son();

            Son.printSign();

            Son sonRel = new Son("son");

            Son.printSign();

            son.getCharactor();

      }

 

}

 

Father & Son class with the same varible

package test;

 

public class Father {

      String str = "father";

 

      public void show() {

            System.out.println("invoke father variable is: " + str);

      }

}

package test;

 

public class Son extends Father {

      String str = "son";

 

      public void show() {

            System.out.println("invoke variable (use this) is: " + this.str);

            Son.printSign();

            System.out.println("invoke variable (use this) is: " + this.str);

            Son.printSign();

            System.out.println("invoke variable (use super) is: " + super.str);

      }

 

      public static void printSign() {

            System.out.println("====================");

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Son son = new Son();

            son.show();

            Son.printSign();

            Father father = new Father();

            father.show();

      }

 

}

 

Object instance base on inheritance

Invoke method

package test;

 

public class Fruit {

      public void grow() {

            System.out.println("growing");

      }

 

      public void harvest() {

            System.out.println("harvest");

      }

}

package test;

 

public class Apple extends Fruit {

      String str;

 

      public Apple(String str) {

            this.str = str;

      }

 

      public void show() {

            System.out.println(str);

      }

}

package test;

 

public class Peach extends Fruit {

      String str = "peach";

 

      public Peach(String str) {

            this.str = str;

      }

 

      public void show() {

            System.out.println(str);

      }

}

package test;

 

public class Client {

      public static void printSign() {

            System.out.println("=====================");

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Fruit appleIh = new Apple("appleIh");

            appleIh.grow();

            appleIh.harvest();

            Client.printSign();

            Apple apple = new Apple("apple");

            apple.show();

            Client.printSign();

            Fruit peachIh = new Peach("peachIh");

            peachIh.grow();

            peachIh.harvest();

            Client.printSign();

            Peach peach = new Peach("peach");

            peach.show();

      }

 

}

 

Invoke varible

package test;

 

public class Car {

      String str = "car";

}

package test;

 

public class Benz extends Car {

      String str = "Benz";

}

package test;

 

public class Bmw extends Car {

      String str = "BMW";

}

package test;

 

public class Client {

 

      public static void PrintSign() {

            System.out.println("===================");

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Car car = new Car();

            System.out.println(car.str);

            Client.PrintSign();

            Car benz = new Benz();

            System.out.println(benz.str);

            Client.PrintSign();

            Car bmw = new Bmw();

            System.out.println(bmw.str);

      }

 

}

 

Invoke static method

package test;

 

public class Animal {

      public static void dine() {

            System.out.println("dine");

      }

 

      public final static void sleep() {

            System.out.println("sleep");

      }

}

package test;

 

public class Humanity extends Animal {

      public static void dine() {

            System.out.println("dine with wine");

      }

 

      public static void getMoney() {

            System.out.println("get money");

      }

}

package test;

 

public class Client {

      public static void printSign() {

            System.out.println("===================");

      }

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            Animal humanityIn = new Humanity();

            humanityIn.dine();

            humanityIn.sleep();

            Client.printSign();

            Humanity humanity = new Humanity();

            humanity.dine();

            humanity.sleep();

      }

 

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值