(a) Learning summary
1. Read the procedure below to see if you can compile the pass? If not, explain why. How should I modify it? What is the result of running the program? Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around?
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.out.println("GrandParent Created.String:" + string); }}class Parent extends Grandparent { public Parent() { System.out.println("Parent Created"); super("Hello.Grandparent."); }}class Child extends Parent { public Child() { System.out.println("Child Created"); }}public class Test{ public static void main(String args[]) { Child c = new Child(); }}
Operation Result:
The parent class should be super ("hello.grandparent."); Put in the first sentence of the construction method, before the output
Run results after modification
Only child classes can invoke member variables and member methods of the parent class, implement inheritance, and not reverse
2. Read the procedure below, analyze what errors exist in the program, explain why, and how to correct them? What is the result of running the correct program?
···
Class animal{
void Shout () {
System.out.println ("Animal called!");
}
}
Class Dog extends animal{
public void shout () {
System.out.println ("Barking ...!");
}
public void sleep () {
System.out.println ("The Dog Sleeps ...");
}
}
public class test{
public static void Main (String args[]) {
Animal Animal = new Dog ();
Animal.shout ();
Animal.sleep ();
Dog dog = animal;
Dog.sleep ();
Animal animal2 = new Animal ();
Dog = (dog) Animal2;
Dog.shout ();
}
}
···
Run results
The first line of error is the result of a transition, only a subclass inherited or overwrite method is called, and there is no sleep method. To remove it
The second line of error is due to the need to add "(type)" to the next transformation, correct: Dog dog = (dog) animal;
Run results
3. Run the following procedure
···
Class Person {
private String name;
private int age;
Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}
}
public class test{
public static void Main (String args[]) {
Person per = new Person ("Zhang San", 20);
System.out.println (per);
System.out.println (Per.tostring ());
}
}
···
(1) The operation result of the program is as follows, what is the problem?
···
[email protected]
[email protected]
···
The output class object calls the ToString method of the object class by default
(2) So, what is the result of the program running? Use Eclipse to open the source code of the println (per) method and see which methods are called in the method, can you explain the results of this example?
Out of this.
(3) Add the following method to the person class
···
Public String toString () {
Return "Name:" + THIS.name + ", Age:" + this.age;
}
···
Rerun the program, what is the execution result of the program? What's the problem?
Refer to the textbook P229
Run results
Override the ToString method, call the Overwrite method, and print the class name by default call the Overwrite method
4. Car rental companies, taxi types have passenger cars, trucks and pickup three kinds, each car in addition to have a number, name, rent three basic attributes, passenger cars have capacity, goods vehicles have cargo capacity, pickup is both carrying capacity and cargo capacity. This paper analyzes the above problems with object-oriented programming, and represents them as appropriate classes, abstract classes or interfaces, and illustrates the design ideas. Now to create a list of rental cars, how should I create them?
Create ideas:
Define the number, name, rent as Interface 1, carrying capacity of one interface 2, cargo capacity as an interface 3
Bus class connection Ports 1 and 2, truck connection ports 1 and 3, Pickup connection interface
5. Read the procedure below to analyze if the code can be compiled, if not, explain why, and make corrections. If you can, list the results of the operation
···
Interface animal{
void Breathe ();
void run ();
void Eat ();
}
Class Dog implements animal{
public void Breathe () {
System.out.println ("I ' m Breathing");
}
void Eat () {
System.out.println ("I ' m eating");
}
}
public class test{
public static void Main (string[] args) {
Dog dog = new Dog ();
Dog.breathe ();
Dog.eat ();
}
}
···
After modification
···
Package assignment three;
Interface animal{
void Breathe ();
void run ();
void Eat ();
}
Class Dog implements animal{
public void Breathe () {
System.out.println ("I ' m Breathing");
}
public void Run () {
System.out.println ("I ' m Running");
}
public void Eat () {
System.out.println ("I ' m eating");
}
}
public class test{
public static void Main (string[] args) {
Dog dog = new Dog ();
Dog.breathe ();
Dog.eat ();
}
}
···
Run results
6. Other content that needs to be summarized.
Experiment Four
Bank class
··· Package test;
public class Bank {
Static String bankname;//Bank name
private String name;
private String password;
Private double balance;//account balance
Private double turnover;//turnover
static void welcome(){ System.out.println("欢迎来到建设银行");}//开户public Bank(String name,String password,double turnover){ this.name = name; this.password= password; this.turnover = turnover; this.balance = turnover - 10; System.out.println(name+"开户成功"+"余额为:"+balance);}//存款方法public void depoist(double turnover){ balance = balance+turnover; System.out.println(name+"您的账户已存入"+balance+"元"+"当前余额为:"+balance+"元");}//取款方法public void withdrawal(String password,double turnover){ if(this.password!=password) { System.out.println("密码错误,重新输入"); return; } if(balance-turnover>=0) { System.out.println("您已取出"+turnover+"元"+"当前余额为"+(balance-turnover)); } else{ System.out.println("余额不足"); } }static void welcomenext(){ System.out.println("请恁鞋带好随身物品,欢迎下次再来");}
}
public class trade{
public static void Main (string[] args) {
Bank.bankname= "CCB";
Bank.welcome ();
Bank Bank = new Bank ("Feng Run", "123456", 200.0);
bank.depoist(200.0); bank.withdrawal("123456",150.0); bank.withdrawal("654321",150.0); bank.withdrawal("123456",450.0); Bank.welcomenext();}
}
···
Employee class
···
Package shiyan4;
public class Employee {//define member variable
Private String empId;
private String name;
private int age;
Private String sex;
Defining Construction methods
Public Employee () {}
Define member methods Set,get methods
Public String Getempid () {
return empId;
}
public void Setempid (String empId) {
This.empid=empid;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
This.name=name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age=age;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex=sex;
}
Define member Methods Show method
public void Show () {
SYSTEM.OUT.PRINTLN ("Employee number is:" +getempid () + "\ t name is:" +getname () + "\ T Age:" +getage () + "\ T Gender:" +getsex ());
}
}
Package shiyan4;
public class Test {
public static void Main (string[] args) {
Creating objects
Employee E = new Employee ();
E.setempid ("Hebei Agricultural University");
E.setname ("Feng Run");
E.setage (21);
E.setsex ("male");
Invoke Display Information method
E.show ();
}
}
···
Java Job Three