1, guess the number of small games
Packagecn.jiemoxiaodi_02;ImportJava.util.Scanner;/*** Guess number games * *@authorHuli **/ Public classGuessnumberdemo { Public Static voidMain (string[] args) {intNumber = (int) (Math.random () * 100) + 1; while(true) {System.out.println ("Please enter the number you guessed:"); String Line=NewScanner (system.in). nextline (); intValue =Integer.parseint (line); if(Number >value) {System.out.println ("Your guess is small."); } if(Number = =value) {System.out.println ("You guessed right."); Break; } if(Number <value) {System.out.println ("You guessed the big."); } } }}
Inheritance: Extends
继承的好处:
A:提高了代码的复用性
B:提高了代码的维护性
C:让类与类产生了一个关系,是多态的前提
继承的弊端:
A:让类的耦合性增强。这样某个类的改变,就会影响其他和该类相关的类。
原则:低耦合,高内聚。
耦合:类与类的关系
内聚:自己完成某件事情的能力
B:打破了封装性
Java中继承的特点
A:Java中类只支持单继承
B:Java中可以多层(重)继承(继承体系)
继承的注意事项:
A:子类不能继承父类的私有成员
B:子类不能继承父类的构造方法,但是可以通过super去访问
C:不要为了部分功能而去继承
两个面试题目:
A:Override和Overload的区别?Overload是否可以改变返回值类型?
Overwrite: The subclass inherits the name of the parent class without the argument function. When a subclass inherits a parameterless function from the parent class and defines an identical parameterless function, the method defined by the subclass overrides the parent class's method, called overwrite.
Overloading: Subclasses inherit the parent class with the same name as the parameter function. When a subclass inherits a method of the same name of the parent class, and the method parameter is different, it is called overloading. By overloading a method, a subclass can re-implement some of the methods of the parent class so that it has its own characteristics.
Override: Method with the same name as the current class. By overriding a method, a class can have multiple methods with the same name, which is determined by the arguments passed to them in different numbers and types. Therefore, the overridden name is a function with the same name in the current class, not a function name in the parent class.
B:this和super的区别和各自的作用?
1) Super (parameter): Call one of the constructors in the base class (should be the first statement in the constructor)
2) This (parameter): Invokes another form of the constructor in this class (should be the first statement in the constructor)
3) Super: It refers to members in the immediate parent class of the current object (used to access member data or functions in the parent class that are hidden in the immediate parent class, when the base class has the same member definition as in the derived class, such as: Super. Variable name super. member function name (argument)
4) This: it represents the current object name (which is prone to two semantics in the program, should use this to indicate the current object, and if the function's shape participates in the class member data with the same name, this is required to indicate the member variable name)
5) call Super () must be written in the first row of the subclass construction method, otherwise the compilation does not pass. The first statement of each subclass construction method is implicitly called super (), and if the parent does not have this form of constructor, it will be error-free at compile time.
6) Super () is similar to this (), except that super () calls the constructor of the parent class from the subclass, and this () invokes other methods within the same class.
7) Super () and this () must be placed in the first line of the construction method.
8) Although you can call a constructor with this, you cannot call two.
9) This and super can not appear in a constructor at the same time, because this is bound to call other constructors, the other constructors will inevitably have a super statement exists, so in the same constructor has the same statement, it loses the meaning of the statement, the compiler will not pass.
This () and super () all refer to objects, so they cannot be used in a static environment. Includes: static variable, static method, static statement block.
11) In essence, this is a pointer to this object, but super is a Java keyword.
Case of the class
A:学生和老师案例
Packagecn.jiemoxiaodi_02; Public InterfacePerson { Public Abstract voideat (); Public Abstract voidstudy ();} Packagecn.jiemoxiaodi_02; Public classStudentImplementsPerson {@Override Public voideat () {System.out.println ("Students eat"); } @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("Student Learning"); } Public voidgame () {System.out.println ("Students play games"); }} Packagecn.jiemoxiaodi_02; Public classTeacherImplementsPerson {@Override Public voideat () {System.out.println ("The Teacher Eats"); } @Override Public voidStudy () {SYSTEM.OUT.PRINTLN ("Teacher Study"); } Public voidTech () {System.out.println ("Teacher teaches"); }} Packagecn.jiemoxiaodi_02;/*** Student Teacher Case * *@authorHuli **/ Public classPersondemo { Public Static voidMain (string[] args) {Student s=NewStudent (); Teacher T=NewTeacher (); S.eat (); S.game (); S.study (); T.eat (); T.tech (); T.study (); }}
B:猫狗案例的分析和实现(略)
Wrapper inheritance polymorphism for Java classes