The fifth chapter Object-Oriented

1. Description of object-oriented and process-oriented distinction and connection?


Both are software development thinking, first, process-oriented, after the object-oriented. In large projects, for lack of process-oriented object-oriented development ideas introduced.
Analogy
process is for fried rice, rice bowl is object-oriented. Benefit rice bowl is the "food", "food" separation, thereby increasing the flexibility of making rice bowl. Rice satisfied with the change of rice, vegetables are not satisfied with changing dishes. In technical terms of software engineering is the "maintainability" is better, "rice" and "food" coupling is relatively low.
Distinction
programming ideas different: process-oriented to achieve the main function of the development of the function, to the first object-oriented abstract classes, attributes and methods, and then completion by instantiating the class, perform the method.
Encapsulation: encapsulation has, but the process is for the package are functional, and the object-oriented data and functions are encapsulated.
Object-oriented have inheritance and polymorphism, but not process-oriented inheritance and polymorphism, object-oriented advantages are so obvious.

2. The relationship between objects and classes are?


Class is abstract

The object is concrete, physical expression is kind of abstract concepts

3. Stack feature is? What store?


Stack features: advanced after
some basic types of variables and objects defined in the function of reference variables are allocated on the stack memory function.

4. piled characterized? What store?

Heap memory features:
heap memory is a memory address, the address of the memory are assigned to the reference variable
heap memory variables after useless garbage collection from time to time by the recovery of
heap memory is automatically initialized

Heap memory storing reference data type, and new out there are heap memory

[5] shown below the machine program memory structure (class code):


//测试类和对象
public class TestObject{
public static void main(String[] args){
Car c1 = new Car();
c1.changeColor("红色");
c1.showColor();
System.out.println(Car.tyreNum);
System.out.println(c1.tyreNum);

Car c2 = new Car();
Engine e = new Engine();
e.speed = 1000;
e.weight = 10;
c2.engine = e;
c2.color="黑色";
c2.tyreNum = 10;
System.out.println(c1.tyreNum);
}
}
class Car {
static int tyreNum=4;
Engine engine;
String color; //char sequence :字符序列

void changeColor(String c){
color = c;
}

void showColor(){
System.out.println ( "My color:" Color +);
}
}
class Engine {
int Speed;
int weight;
}

 


6. local variables, member variables, static variables are declared how?


Local variables: variables declared in the code block or a method referred to as local variables
member variables: the variables declared in the class member variable called
static variables: variables modified static member called a static variable

7. Before use local variables must be manually initialize it?


Before you can use local variables must be initialized manually

8. If the values ​​of the member variables manually specified, the system will automatically initialized. So initialization rule is that?


Integer initialized to zero
float initialized to 0.0
reference data types are initialized to null
boolean types are initialized to false
char type is initialized to \ u0000

9. Who belonging to member variables? What are known as static variables and subordinate to whom? Who belongs to a local variable?


Member variable objects belonging to
a static class variables belonging to the class called variable
local variable dependent methods or code blocks

10. The construction method is to effect two: an object class structure, the other function is to initialize the object attribute. This argument is not it?

Correct

11. The name of the constructor must be consistent with the class name?


We must be consistent with the class name

12. constructor has no return value? With a detailed text description of return value.


Constructor no return value along, you can not write void

13. How constructor is called?

Constructor is called when the object is created using the new keyword

 

14. constructor can not have a return statement?


Constructor can not use the return statement

15. The system will give us added no-arg constructor do? Please explain.


If after the manual preparation of the construction method, the system will not be added without a constructor parameter

16. What's wrong with the following code:


class Computer {
int price;
int type;
String brand;

public void start(){
System.out.println("启动中....");
}

computer(int _price, int type, String _brand){
//this.price = price;
price = _price;
type = type;
brand = _brand;
}
}

When the local variable name and the member variable with the same name, the use of this variable behalf of the members, the code should be changed to
Computer (int. Price, int type, String Brand) {
this.price =. Price;
this.type = type;
this.brand = Brand ;
}

[17] Design of a machine class represents a point in two-dimensional space


public class Point{
private int x;
private int y;
public void setX(int x){
this.x=x;
}
public int getX(){
return x;
}
public void setY(int y){
this.y=y;
}
public int getY(){
return y;
}
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
}

18. [machine] design a circle represents a class of two-dimensional space. It requires two member variables. Is a circle, the radius is to provide a method to calculate the area.


public class Cricle{
private int r;
private Point point;
public void setR(int r){
this.r=r;
}
public int getR(){
return r;
}
public void setPoint(Point point){
this.point=point;
}
pubic Point getPoint(){
return point;
}
public Cricle(){

}
public Cricle(int r,Point point){
this.r=r;
this.point=point;
}
public double area(){
return Math.PI*Math.pow(r,2);
}
}


19. A machine [a] method of adding the above-described Cricle class, calculating a point (Point Object) in a circle (Cricle
within the object), and the write program verification

// Method in Cricle added class
public class Cricle {
... ..
public Boolean isFlag (Point P) {
IF (Math.pow (xp.getX ()) + Math.pow (Y-getY ()) <= the Math. POW (R & lt, 2)) {
return to true;
}
}
return to false;
}
public class the Test {
public static void main (String [] args) {
Point new new Point P = (3,3); // center
Cricle c = new Cricle (. 5, P);
System.out.println (c.isFlag (new new Point (5, 5)) "in a circle"?: "is not within circle");
}
}

20. constructor can not be overloaded?


Constructors can be overloaded

21.this in the conventional method, which refers to an object? In the constructor, referring?


In general this method always points to the method is called object
this in the constructor is always going to point to the object initialization

22.static variables placed in the area in which memory? Static variables and methods Why are called class variables and class side
method? It can be shared by all such objects?


there is a data area of static variables
static variables and methods are classified all, the class name to invoke
all of the objects can be shared by the class

23. The static initialization block and the main method which is performed first?


Static initialization block to execute

24. a constructor call another constructor how to call? This (), must be located in such a way to call the first
word?


this () calls this class constructor with no arguments
this (argument list) calls this class parameterized constructor
super () calls the constructor with no arguments direct parent of
super (argument list) call with reference direct parent of constructor
this () constructor calls must be located in the first sentence

What role 25.package two is?


Resolve duplicate names between classes
in order to facilitate the management class: class located right package suitable

26. After the increase in package, we compiled under DOS how to do?


javac -d . Test.java

27.import package is used to import or import the class?


import package is for introducing the other class

. 28.import Java.util *; will slowdown the program Why??


Does not reduce the running speed
because it is introduced in all classes of the package, it can slow the build

29.import static effect static import is a static property and static introduction method for introducing a class or classes


Static properties and static methods of introducing class

30. If you import the same time: Java.util.Date; Java.sql.Date; how to distinguish between them in the program?


+ Class package name can only be used to display the name of the calling Class
java.util.Date date = new java.util.Date ();

[31] machine completed this, static, package, import of test code


package cn.sxt.pro
import java.util.Scanner;
public class Test{
public static int age;
private String name;
public void setName(String name){
this.name=name;
}
public static void show(){
}
public static void main(String [] args){
System.out.println(Test.age);
System.out.println(Test.show());
}
}

32.Javadoc comments how to write?


/ * Beginning
* / End

How to generate API documentation 33.Java project? Please write step.


javadoc source file name .java;

34. A packaging object-oriented, the pursuit of "high cohesion and low coupling." Explain what cohesion? What
couple?


Cohesion: the relationship between the various modules in the program how closely
coupled: the relationship between the external program (subroutine) the closeness

35. The package of a few keywords: private, default, protected, public meaning.


Represents access to
private: private and only this type of access
default: By default, this class, subclass, this package of other classes can access
protected: protected, this class, this package and other packages the subclasses can access
public: public, this class, subclass, the present package, and other classes can access

36. The general property is to be set to private?


That the property can not be assigned a value on any unlawful

37. If the property is set to private, how to get external access to the property?


Write the value of the public get method

38. provide getter, setter methods of operation, eclipse how to achieve?


The menu bar SourceGenerate getters and setters

39. For boolean types of property, getter methods are provided: getXXX or isXXX?


isXXX;

 

40.Javabean that contains only the properties and related getter, setter methods, do not contain business logic processing classes, this argument right?


Not
Javabean encapsulated data may be also be encapsulate business logic

41. What are the benefits of inheritance is?


A benefit: the inherited nature is abstract. Class is an abstract object, inheritance is an abstract of a group of class
two benefits: In order to improve the code reusability

42.Java are multiple inherited it? C ++ has multiple inheritance it? Advantages over single inheritance multiple inheritance in what areas?


java only single inheritance
in C ++ multiple inheritance
using a single inheritance in favor of maintaining the system, making the relationship between class and class simple

43. [machine] imitate the example of the animal world class, we wrote an example.


class Person{

}
class Teacher extends Person{

}

44.Java inheritance which keywords to use to achieve?


extends

45. The definition of a class, if not using extends, which inherited class?


If you do not use extends, then inherited the Object class

46.Java, there is no multiple inheritance? Please explain why Java canceled multiple inheritance?


no multiple inheritance in java
using single inheritance in favor of maintaining the system, making the relationship between class and class simple

47. rewriting (override) refers to what?


It refers to a subclass overrides methods of the parent class

 

48. If the parent class has a main method, a subclass can inherit?


Can not

 

49.Object is the root class of all classes do? Is the direct parent of all classes do?


Object is the root of all classes that
are not the direct parent of all classes

 

50. Where to view the source code for the Object class?


jdk installation directory src compression package can view the source code

 

toString method 51.Object class can overridden by subclasses? Please do the test.


can

 

52. [machine] to complete inheritance, Object-related test code

53.super refers to what?


super refers to the direct parent

54. The method of construction, the first words are always super it?


It may be this ()

 

55. describe what, when inheritance structure, calling sequence constructor?


Calling convention constructor inherited conditions are as follows:
If the constructor subclass not by referring to the constructor has super explicitly call parent class, there is no explicit calls itself other constructors by the this, the system will default to call parent class constructor with no arguments. In this case, write do not write "super ();" statement, the effect is the same.
If the subclass constructor argument constructor methods super explicitly by calling the parent class, it will perform the corresponding parent class constructor, without performing the parent class constructor with no arguments.
If explicit calls itself Constructor for other subclass by this, the above two rules apply in a corresponding constructor.
Of particular note is that if there is multi-level inheritance, when you create a subclass object, the above rules several times to a higher level parent application, has been up to no-argument constructor to perform top-level parent class Object class.

56. rewritten, the subclass return type Can not entirely consistent with the parent? State your reasons.


Can
subclass return value may be the same parent, the parent may be a subclass of the class method returns a value of

 

57. How to determine the contents of two objects are the same? Rewrite equals () What are the requirements?


Analyzing two objects have the same contents, it is necessary to rewrite the equals method of Object class

58. For a practical example described polymorphism.


The first time I went to see the wife's mother. Not see, I have put my girlfriend briefing to the wife's mother, that I "looks like Hong Kong stars." After meeting wife's mother, wow, it turned out to be like Eric Tsang. She fainted on the spot.

 

59. What are the three necessary conditions for polymorphism is?


Inherited
method overrides
the parent class references to subclass objects

60. What is the upward transition? What is downcast?


Upcast: references to the parent class subclass object
downcast: the parent class reference turn into real subclass object

Common use cases 61. polymorphic What?


Made using the parent class method parameter, arguments may be any type of a subclass of the parent class
using the method of the parent class does value subclass object may be arbitrarily modified return type parent, return

62.Java method using dynamic binding or binding static binding?


Static methods, constructors, private method, using the keyword super method invocation are statically bound
method object calls, the use of dynamic binding

 

63.final modifying variables, methods, classes represent what meaning?


final modified variables can not be changed once assigned the initial value, it can not be reassigned, called constants
final modification of the method can not be overridden by subclasses
final modified class can not have subclasses

64.final modified method can not be overloaded? It can not be rewritten?


The final modification method can override
final modified method can not be overridden

65.String class can not be inherited? Why?


String class can not be inherited
because the String class is final class of modified

66. The method of containing abstract class is abstract it? Abstract class must contain an abstract way?


Comprising abstract class is abstract method
abstract class does not necessarily imply an abstract method

 

67. abstract classes can not have a common method? Can you define a constructor method?


An abstract class can have an ordinary method
abstract class constructor can be defined in

68. The new abstract class can not be out? That is an abstract class constructor can not be called?


An abstract class can not be out of new
construction methods abstract class called by subclasses

69. The interface can only define constants and abstract methods it?


Before JDK1.7, the interface can only define static public constants and abstract methods
JDK1.8, the interface can be defined static methods

70. The interface describes the real world what logic?


Interface describes the real world is, "If you are ... you must be able to .." thinking
if you're an angel, you must be able to fly; if you are a car, you must run

71. The interface must be public static final constants do? Public abstract abstract method must be right?


Yes

72. The common interface variables, ordinary methods can not be defined? From the perspective of interface design why do it?


Interface not define common variable, the conventional method can not be defined
as a kind of an interface specification that defines a regular

73. Interface can be new?


The interface can not be new

74. There is no interface to multiple inheritance?


How inheritance interface

75. A class can implement multiple interfaces?


A class can implement multiple interfaces

[76.] complete machine abstract class, interface code syntax test


public interface TakePhoto {

}
abstract class Phone{

}
class MoblePhone extends Phone implements TakePhoto{

}
class Test{
public static void main(String[] args) {
MobilePhone mp=new MobilePhone();
}
}

77. [machine] use interface that defines electronic systems (smart phones, MP3, smart watch).


public interface Electronic {

}
class MobilePhone implements Electronic{

}
class Mp3 implements Electronic{

}
class Watch implements Electronic{

}

78. How multiple objects are sorted in an array of different basis;


Comparable interface object that implements
or comparison rules defined class that implements the interface Comparator

Comparable and 79. Comparator interface analog implementation;


实现Comparable接口
public class Student implements Comparable<Student>{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.age-o.getAge();
}
}
实现Comparator接口
class AgeComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
}

80.GC refers to what? Write English name. Garbage collection mechanism, the programmer can call the garbage collector?


GC: refers to garbage collection
English: garbage collection
programmers have no right to call the garbage collector

81.System.gc () refers to what does this mean?


Runs the garbage collector

82.finalize method can be used to do?


Before the objects cleared from memory out of the garbage collector, do the necessary clean-up work

83. C ++ describes two cases, different memory recovery mechanism Java.


A long time ago, every household "minding" its own garbage collection.
Now, the EPA garbage trucks automatic garbage collection.
By these examples of different analogies of Java and C ++ recovery mechanisms and features.
C ++ programmers need to manually write code for garbage collection, if the programmer forgot so it is easy to cause memory overflow issues such as
java without manually writing, jvm garbage collection garbage collection mechanism will automatically reduce the programmer's work, but also to avoid programmer forgets caused by various problems such as memory overflow

Guess you like

Origin www.cnblogs.com/ren549047861/p/11293917.html
Recommended