Write a animal class with attributes: Kind; With function: Eat, sleep. Define its sub-class fish
PackageZhongqiuzuoye; Public classAnimal {//Properties PrivateString type; PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } //function Public voideat () {} Public voidsleep () {}}
package Zhongqiuzuoye; public class Fish extends animal{ public void Eat () {System.out.printl N (GetType () + "eat" ); public void sleep () {System.out.println (GetType () + "Sleep" ); }}
package Zhongqiuzuoye; public class Dog extends animal{ public void Eat () {System.out.printl N (GetType () + "eat" ); public void sleep () {System.out.println (GetType () + "Sleep" ); }}
Package Zhongqiuzuoye; Public class Testanamal { publicstaticvoid main (string[] args) { // TODO Auto-generated method stub fish f=new fish (); F.settype ("fish"); F.eat (); F.sleep (); Dog D=new Dog (); D.settype ("Dog"); D.eat (); D.sleep (); }}
Java Basics-Inheritance topic: Write a Animal class with attributes: kind; function: Eat, sleep. Define its sub-class fish