Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Inheritance, Excercise 01, slide 27
- /*
- * Testrer01 class
- *******************************************************/
- import packege01.Cat;
- import packege01.SiamiCat;
- import packege01.StreetCat;
- public class Testrer01 {
- public static void main(String[] args) {
- Cat c = new Cat("coco", 12, "brown");
- StreetCat sc = new StreetCat("soso", 10, "gold", 2);
- SiamiCat siami = new SiamiCat("siami", 18, "White", "any food");
- System.out.println(c);
- System.out.println(sc);
- System.out.println(siami);
- }
- }
- /*
- * Cat class
- *******************************************************/
- package packege01;
- public class Cat {
- // private properties
- protected String name; // cat name
- protected double whisker;// length of cat whisker in mm
- protected String color; // color of the cat
- public Cat(String name, double whisker, String color) {
- super();
- this.name = name;
- this.whisker = whisker;
- this.color = color;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getWhisker() {
- return this.whisker;
- }
- public void setWhisker(double whisker) {
- this.whisker = whisker;
- }
- public String getColor() {
- return this.color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- @Override
- public String toString() {
- return "Cat [" + fieldsList() + "]";
- }
- protected String fieldsList() {
- return "name=" + name + ", whisker=" + whisker + ", color=" + color;
- }
- }
- /*
- * StreetCat class
- *******************************************************/
- package packege01;
- public class StreetCat extends Cat {
- private int fightsNo; // number of fights
- public StreetCat(String name, double whisker, String color, int fightsNo) {
- super(name, whisker, color);
- if (!this.setFightsNo(fightsNo))
- this.fightsNo = 0;
- }
- public int getFightsNo() {
- return this.fightsNo;
- }
- public boolean setFightsNo(int fightsNo) {
- if (fightsNo < 0) {
- return false;
- }
- this.fightsNo = fightsNo;
- return true;
- }
- @Override
- public String toString() {
- return "StreetCat [" + super.fieldsList() + ", fightsNo=" + fightsNo
- + "]";
- }
- }
- /*
- * SiamiCat class
- *******************************************************/
- package packege01;
- public class SiamiCat extends Cat {
- protected String bestFood; // name of best food
- public SiamiCat(String name, double whisker, String color, String bestFood) {
- super(name, whisker, color);
- this.bestFood = bestFood;
- }
- public String getBestFood() {
- return this.bestFood;
- }
- public void setBestFood(String bestFood) {
- this.bestFood = bestFood;
- }
- @Override
- public String toString() {
- return "SiamiCat ["+super.fieldsList()+", bestFood=" + bestFood + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement