1, If a company wants to develop "XX car store management system", please use the object-oriented idea to design custom classes to describe bicycles, electric vehicles and tricycles. The program reference operation effect diagram is as follows:
Task analysis:
Step 1: analyze the commonalities of bicycles, electric vehicles and tricycles:
(1) Both and are non motor vehicles, with the basic characteristics of non motor vehicles
(2) Both have operation methods
Step 2: define non motor vehicles according to commonness
Attributes: brand, color, wheel (2 by default), seat (1 by default)
method:
(1) . compile the nonparametric construction method, double parameter construction method and four parameter construction method, in which the assignment of brand and color is completed; In the four parameter construction method, complete the assignment of all attributes
(2) Write the operation method. The description content is: This is a xx color, xx brand non motor vehicle with xx wheels and xx seats. The data of xx is provided by the attribute
Step 3: define that bicycles, electric vehicles and tricycles inherit bicycles respectively. Requirements:
(1) . bicycles:
First, call the parent class multi parameter structure in the construction method to complete the attribute assignment.
② Rewrite the operation method. The description is: This is a xx color, xx brand bicycle. The data of xx is provided by the attribute
(2) . electric vehicle:
① . add "battery brand" attribute
② Rewrite the operation method, and the description is as follows: This is an electric vehicle using xx brand battery. The data of xx is provided by the attribute
(3) . tricycle:
① . modify the wheel attribute value in parameterless construction
② Rewrite the operation method, and the description is as follows: tricycle is a non motor vehicle with xx wheels. The data of xx is provided by the attribute
Parent class
package com.hopu; //Parent class public class Car { private String color;// colour private String type;// brand private int wheel;// wheel private int seat;// chair public Car() { } public Car(String color, String type) { this.setColor(color); this.setType(type); } public Car(String color, String type, int wheel, int seat) { this.color = color; this.type = type; this.wheel = wheel; this.seat = seat; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getWheel() { return wheel; } public void setWheel(int wheel) { this.wheel = wheel; } public int getSeat() { return seat; } public void setSeat(int seat) { this.seat = seat; } public String info() { String str = "Parent class information test: This is a car" + this.getColor() + "Color," + this.getType() + "License non motor vehicles, yes" + this.getWheel() + "Wheels, yes" + this.getSeat() + "Seats"; return str; } }
Bicycle
package com.hopu; public class Bike extends Car{ public void Bike(){ } // public Bike(String color,String type){ super(color,type); System.out.println("Bicycle information test: This is a bicycle" + color + "Color," + type + "Brand bicycle"); } }
Electric vehicle
package com.hopu; public class Electrocar { private String cell; public Electrocar() { } public String getCell() { return cell; } public void setCell(String cell) { this.cell = cell; } public Electrocar(String cell){ super(); this.cell=cell; System.out.println("Electric vehicle information test: This is a vehicle used" + this.getCell() + "Brand battery electric vehicle"); } }
Tricycle
package com.hopu; public class Tricycle extends Car{ public Tricycle(){ super(); this.setWheel(3); } public String sl() { String str = "Tricycle test information: tricycle is a kind of" + this.getWheel() + "Non motor vehicle with two wheels"; return str; } }
Test class
package com.hopu; public class Test { public static void main(String[] args) { Car car=new Car("black","public",4,4); System.out.println(car.info()); Bike bike=new Bike("gules","a mountain country"); Electrocar electrocar=new Electrocar("emma "); Tricycle tricycle=new Tricycle(); System.out.println(tricycle.sl()); } }
2, Please use the Object-oriented idea to design a custom class, Person inherits the Object class, and override the toString method to output Object information.
The operation effect is shown in the figure below:
Train of thought analysis
(1) . create a Person class that inherits from Object. The class structure requirements are:
Attributes: name, age, sex
method:
① . create a construction method with parameters (name, age, sex as parameters)
② . rewrite the toString method, and the output information format is: Name: xx age: xx gender: xx (where xx is the corresponding attribute value of the object)
(2) Create a test class, instantiate the Person object in the test method, and pass in three attribute values. Then, print out two lines of object information by directly printing the Person object and using the rewritten toString method.
Custom class
package com.hopu.demo2; public class Person { private String name; private int age; private String sex; public Person() { } public Person(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
Test class
package com.hopu.demo2; public class Test { public static void main(String[] args) { Person person=new Person("Star clusters",3000,"male"); System.out.println(person); System.out.println(person.toString()); } }
3, Please use the object-oriented idea to realize the information description of Myrica rubra and Xianren banana.
The program reference operation effect diagram is as follows:
Train of thought analysis:
(1) . according to the commonness of Myrica rubra and banana, the parent fruit is extracted
Private attributes: shape and taste of fruit
method:
① . constructor with parameters (the parameters are shape and taste)
② . create a method eat with no parameters and no return value (the description is: fruit is available for people to eat!)
③ . override the equals method to compare whether two objects are equal (compare shape, taste)
(2) , subclass Waxberry
Private attribute: color
method:
① . call the constructor of the parent class to complete the attribute assignment
② . create a face method that does not allow rewriting. The description is: bayberry: xx, xx, with moderate sour and sweet fruit flavor.
③ Rewrite the parent eat method, which is described as: myristic acid is sweet and delicious!
④ . override the toString method, and the output is expressed in different forms (output shape,color,taste)
⑤ . requires that the Waxberry class cannot have subclasses
(3) . subclass: Banana
Private attribute: variety
method:
① . the construction method with parameters assigns values to all attributes
② . create an advance method without parameters and return value, which is described as: xx fruit shape, xx, sweet pulp, and can be eaten raw.
③ . the advanced method (with parameter color) in overload requirement (2) is described as xx, and the color is xx
(4) , test and operation effect refer to the effect drawing:
① . instantiate two parent class objects and pass in two sets of the same parameter values
② . call the parent eat method
③ . test overriding the equals method to determine whether two objects are equal
④ . instantiate the subclass Waxberry object and pass in the relevant parameter values
⑤ . call the subclass face method and the eat method after overriding the parent eat method
⑥ . the test rewrites the toString method to output the information of subclass objects
⑦ Instantiate the Banana class object and pass in the relevant parameter values
⑧ . call the advance of the subclass and its overloaded methods
--------
Copyright notice: This is the original article of CSDN blogger "ymj960722", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Original link: https://blog.csdn.net/ymj960722/article/details/107932698
The code is as follows:
Parent class
package com.hopu.demo3; import java.util.Objects; public class Fruits { // Member attributes: shape and taste of fruit private String shape; private String taste; public Fruits() { } public Fruits(String shape, String taste) { this.shape = shape; this.taste = taste; } public String getShape() { return shape; } public void setShape(String shape) { this.shape = shape; } public String getTaste() { return taste; } public void setTaste(String taste) { this.taste = taste; } // Create a method eat with no parameters and no return value public void eat() { System.out.println("Fruit is available for people to eat!"); } // Override the equals method @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Fruits other = (Fruits) obj; if (shape == null) { if (other.shape != null) return false; } else if (!shape.equals(other.shape)) return false; if (taste == null) { if (other.taste != null) return false; } else if (!taste.equals(other.taste)) return false; return true; } }
Subclass Myrica rubra
package com.hopu.demo3; public class Waxberry extends Fruits { private String color; // Nonparametric construction method public Waxberry() { } // Structural method with parameters public Waxberry(String shape, String taste, String color) { super(shape, taste); this.setColor(color); } // get/set public String getColor() { return color; } public void setColor(String color) { this.color = color; } // Create a face method that does not allow overriding public final void face() { System.out.println("Red bayberry:" + this.getColor() + "," + this.getShape() + "," + "Fruity" + this.getTaste() + ". "); } // Override the parent eat method @Override public void eat() { System.out.println("Myristic acid is sweet and delicious!"); } // Override toString method @Override public String toString() { String str = "Information of red bayberry: the fruit is" + this.getShape() + "," + this.getColor() + "," + this.getTaste() + ",Very delicious!"; return str; } }
Subclass banana
package com.hopu.demo3; public class Banana extends Fruits{ // Private attribute: variety private String variety; // Nonparametric structure public Banana() { } // Parametric structure public Banana(String shape, String taste, String variety) { super(shape, taste); this.setVariety(variety); } // get/set public String getVariety() { return variety; } public void setVariety(String variety) { this.variety = variety; } // Create an advance method with no parameters and no return value public void advantage() { System.out.println(this.getVariety() + "Fruit shape" + this.getShape() + ",flesh" + this.getTaste() + ",Available raw."); } // Overloaded advanced method public void advantage(String color) { System.out.println(this.getVariety() + "Color is" + color); } }
Test class
package com.hopu.demo3; public class Test { public static void main(String[] args) { Fruits fru1 = new Fruits("circular", "Moderate sour and sweet"); Fruits fru2 = new Fruits("circular", "Moderate sour and sweet"); // Call the parent eat method fru1.eat(); // The test overrides the equals method to determine whether two objects are equal System.out.println("fru1 and fru2 Comparison of references:" + fru1.equals(fru2)); System.out.println("-------------------"); // Instantiate the subclass Waxberry object and pass in the relevant parameter values Waxberry wb = new Waxberry("circular", "Moderate sour and sweet", "Purplish red"); // Call the subclass face method and the eat method after overriding the parent eat method wb.face(); wb.eat(); // The test rewrites the toString method to output the information of subclass objects System.out.println(wb); System.out.println("-------------------"); // Instantiate the Banana class object and pass in the relevant parameter values Banana bn = new Banana("Short and slightly round", "fragrant and sweet", "Immortal banana"); // Call the advance of the subclass and its overloaded methods bn.advantage(); bn.advantage("yellow"); } }