2 inheritance

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The test class:

package cn.jbit.epettest;

import java.util.Scanner;

import cn.jbit.epetshop.Dog;
import cn.jbit.epetshop.Penguin;
import cn.jbit.epetshop.Pet;

public class EpetShopTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Welcome to the pet shop!");
		System.out.print("Please input the pet name: ");
		String name = input.next(); // Pet names

		System.out.print("Please choose to adopt pets types: (1, 2, penguin, dog)");
		String type = input.next(); // Pet type

		Pet pet = null; // The definition of a pet object
		if (type.equals("1")) {
			System.out.print("Please select the dog breed: (1, smart Labrador 2, cool Xue Narui)");
			String choiceStrain = input.next(); // Select the kind of pet
			String strain = null; // Pet breeding
			if (choiceStrain.equals("1")) {
				strain = "Smart Labrador";
			} else {
				strain = "Cool Xue Narui";
			}

			// Create a pet dog object
			pet = new Dog(name, 100, 0, strain);

		} else if (type.equals("2")) {
			System.out.print("Please select the penguin sex: (1, Q, 2, Q)");
			String choiceSex = input.next(); // Choose a pet sex
			String sex = null; // Pet sex
			if (choiceSex.equals("1")) {
				sex = Penguin.SEX_MALE;
			} else {
				sex = Penguin.SEX_FEMALE;
			}

			// Create a pet penguin object
			pet = new Penguin(name, 100, 0, sex);
		}

		// Method invocation output information
		pet.print();
	}

}

 

The entity class:

 

package cn.jbit.epetshop;

/**
 * Pet
 * 
 * @author chenguiju
 * @2013-10-17 09:25:09 in the morning
 */
public abstract class Pet {
	/**
	 * Parameterless constructor
	 */
	public Pet() {

	}

	/**
	 * Parameterized constructor
	 * 
	 * @param name
	 *            Nickname
	 * @param health
	 *            Health value
	 * @param love
	 *            Intimacy
	 */
	public Pet(String name, int health, int love) {
		this.setName(name);
		this.setHealth(health);
		this.setLove(love);
	}

	private String name; // Nickname

	/**
	 * Read the nickname
	 * 
	 * @Return nickname
	 */
	public String getName() {
		return name;
	}

	/**
	 * Specify the nickname
	 * 
	 * @param name
	 *            Nickname
	 */
	public void setName(String name) {
		this.name = name;
	}

	private int health = 100; // Health value

	/**
	 * Read health value
	 * 
	 * @Return health value
	 */
	public int getHealth() {
		return health;
	}

	/**
	 * Specify the health value
	 * 
	 * @param health
	 *            Health value
	 */
	public void setHealth(int health) {
		if (health >= 0 && health <= 100) {
			this.health = health;
		} else {
			health = 40;
			System.out.println("Health value should be between 0~100, the default value is 40");
		}

	}

	private int love = 0; // Intimacy

	/**
	 * Read the intimacy
	 * 
	 * @Return intimacy
	 */
	public int getLove() {
		return love;
	}

	/**
	 * Specify the intimacy
	 * 
	 * @param love
	 *            Intimacy
	 */
	public void setLove(int love) {
		this.love = love;
	}

	/**
	 * Output information
	 */
	public abstract void print();
}

 

package cn.jbit.epetshop;

/**
 * The pet dog class
 * 
 * @author Administrator
 * 
 */
public class Dog extends Pet {

	private String strain; // Varieties

	/**
	 * Read varieties
	 * 
	 * @Return varieties
	 */
	public String getStrain() {
		return strain;
	}

	/**
	 * The specified varieties
	 * 
	 * @param love
	 *            Varieties
	 */
	public void setStrain(String strain) {
		this.strain = strain;
	}

	/**
	 * Parameterless constructor
	 */
	public Dog() {
	}

	/**
	 * Parameterized constructor
	 * 
	 * @param name
	 *            Nickname
	 * @param health
	 *            Health value
	 * @param love
	 *            Intimacy
	 * @param strain
	 *            Varieties
	 */
	public Dog(String name, int health, int love, String strain) {
		super(name, health, love);
		this.setStrain(strain);
	}

	/**
	 * The dog information output
	 */
	public void print() {
		System.out.println("Pet confession: my name is "\n (+ super.getName) +", health value is"
				+ super.getHealth() + ", And the owner of the intimacy is "+ (super.getLove) +", I am a"
				+ this.strain + ". ");
	}

}

 

package cn.jbit.epetshop;

/**
 * Pet penguin class
 * 
 * @author Administrator
 * 
 */
public class Penguin extends Pet {

	public final static String SEX_MALE = "Q Zi "; // gender
	public final static String SEX_FEMALE = "Q sister "; // gender
	private String sex = SEX_FEMALE; // Gender

	/**
	 * Read sex
	 * 
	 * @Return sex
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * Specify the gender
	 * 
	 * @param love
	 *            Gender
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * Non parametric method
	 */
	public Penguin() {
	}

	/**
	 * The parametric method
	 * 
	 * @param name
	 *            Nickname
	 * @param health
	 *            Health value
	 * @param love
	 *            Intimacy
	 * @param sex
	 *            Gender
	 */
	public Penguin(String name, int health, int love, String sex) {
		super(name, health, love);
		this.setSex(sex);
	}

	/**
	 * Penguin information output
	 */
	public void print() {
		System.out.println("Pet confession: my name is "\n (+ super.getName) +", health value is"
				+ super.getHealth() + ", And the owner of the intimacy is "+ (super.getLove) +", the gender is"
				+ this.sex + ". ");
	}
}

 

public SubClass extends SuperClass{
    //A class definition
}
In Java, the inheritance (Inheritance) is realized by the extends keyword, which SubClass is called the subclass, SuperClass said the superclass, base class or superclass. The modifier if it is public, the visible throughout the project; don't write the public modifier is the only visible in the current package; can't use private and protected modified class.
 
Inheritance is one of the three characteristics of kind, is one of the important means to achieve code reuse in Java. Single inheritance only supports Java, each class can have only one direct superclass. Is a expression of the inheritance relationship, or is a special and general relation, e.g. Dog is a Pet. Two we could let the students make apple fruit heir, inheritance, inheritance let triangle geometry.
 
In Java, a subclass can inherit from parent classes to which "property"?
Super represents the default direct superclass object for the current object references. Members of the subclass can through the super keyword to access the parent class.


For example, in the Dog class can access the parent class members through the following statement.
super.name, //Access to the name attributes of the parent class (if name is private privileges, you cannot access)
super.print(), //Access class (print) method
super(name), //Access method corresponding structure direct superclass, can only occur in the constructing method.
 
Inherited tectonic conditions were calling the rules are as follows.

Inheritance is one of the important means to achieve code reuse in Java. Single inheritance only supports Java, a class can have only one direct superclass. The java.lang.Object class is the ancestor of all Java.
In a subclass can be re written to the methods inherited from a parent class according to the actual demand, a method called rewriting or cover.
Overriding methods in subclasses and superclasses overridden method must have the same name, list of parameters, return value type must be overridden method return value type is the same or sub class.
The construction method in the construction method of sub class without explicitly through super call the parent class, nor by this significant other structural method calls itself, non parametric method to construct the system will default to call the parent class.
Abstract class cannot be instantiated. Abstract class can not, can have one or more abstract method. Subclasses must override all abstract methods can be instantiated, otherwise the sub class is an abstract class.
Modified by final, and cannot be inherited. Method modified by final, cannot be overridden by subclasses. Modified by final variable will become constant, only one assignment.

Personal E-mail: chaoyi77@163.com

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Angelina at December 15, 2013 - 5:42 PM