1. What is a construction method? What is an overload of a constructor method? Can the following program be compiled? Why?
~ Publicclass Test {
public static void Main (String args[]) {
Foo obj = new Foo ();
}
}
Class foo{
int value;
Public Foo (int intvalue) {
value = Intvalue;
}
}~
No, because the program did not create the constructor foo ();
There are multiple parameters or different types of parameters cannot be overloaded;
If the construction method is not explicitly defined in the class, the default constructor method is called automatically;
2. What is the result of running the following procedure? Analyze why, how to modify
~ Publicclass Test {
public static void Main (string[] args) {
Myclass[] Arr=new myclass[3];
arr[1].value=100;
}
}
Class myclass{
public int value=1;
}~
Result: The program cannot be displayed
Cause: The MyClass array is not initialized properly, it can be initialized with a loop before arr[1].value=100;
3. What is the result of running the following procedure? explain why.
~ Publicclass Test {
public static void Main (string[] args) {
Foo obj1 = new Foo ();
Foo obj2 = new Foo ();
System.out.println (obj1 = = obj2);
}
}
Class foo{
int value = 100;
}~
The result is flase, the reason: The address of the two is different;
4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
Encapsulation is simply putting properties and methods together and then depositing them into the same object. By encapsulation, data access restrictions on attributes can be achieved, and the maintainability of the program is increased.
~~~public class Worker {
private int No;
Private Datet Bir;
Private Home whome;
}
Public Worker (String sname,int n,datet s) {
This.sname=sname; This.n=n; This.s=s
}
public void Setbir (Datet s) {
This.s=s;
} ~~~
5. Read the procedure below to see if you can compile the pass? If not, explain why.
~ class a{
Private int secret = 5;
}
public class test{
public static void Main (String args[]) {
A A = new A ();
System.out.println (a.secret++);
}
} ~
cannot, cause: Serect is encapsulated in a and cannot be manipulated directly within the main class function. Secret
6. Using static variables and construction methods of a class, you can track the number of objects created by a class. Declare a book class, the data members are numbered, the title, the book Price, and the number of static data member book record number of books. Book numbering starts with 1000, and each object is generated, the number is automatically incremented (implemented using static variables and construction methods). Here is a partial code for the test class code and the book class, which complements the code as a whole.
~~~package Teat1;
Class book{
int bookId;
String BookName;
Double Price;
static int m;
declaring static variables
Static
{
m=1000;
}
Defines static code blocks to initialize static variables
Public Book (String q,double N)
{
This.setbookname (q);
This.setprice (n);
This.bookid=m;
m=m+1;
}//Construction method
Public String Getbookname () {
return bookname;
}
public void Setbookname (String bookname) {
This.bookname = BookName;
}
Public double GetPrice () {
return price;
}
public void Setprice (double price) {
This.price = Price;
}
How to find the total number of books by definition method
public static int Totalbook ()
{
return m-1000;}//重写toString方法public String toString (){ return "编号\t"+bookId+"书名\t"+getBookName()+"价格\t"+getPrice();}
}
public class test{
public static void Main (String args[]) {
book[] Books = {new book ("C Language Programming", 29.3),
New book ("Database Principle", 30),
New book ("Java Learning note", 68)};
SYSTEM.OUT.PRINTLN ("Total number of books:" + Book.totalbook ());
for (book Book:books) {
System.out.println (Book.tostring ());
}
} `
}~~~
7. What is a singleton design pattern? What characteristics does it have? Design a solar class sun with a single-instance design pattern.
A class has and only one instance, and self-instantiation is provided to the entire system, instance control: Features: Good flexibility, a class can have only one instance, and this instance must be built by itself;
~~~public class A {
private static Sun p = null;
Private A () {
}public synchronized static A getp() { if (p == null) { p = new A(); } return p;}
}~~~
8. Understand the Java parameter passing mechanism, read the following program, what is the result of running? explain why.
~~~
public class Test {
String str = new string ("Hello");
char[] ch = {' W ', ' O ', ' l ', ' l ', ' d '};
public static void Main (String args[]) {
Test test = new test ();
Test.change (Test.str, test.ch);
System.out.print (TEST.STR);
System.out.print (test.ch);
}
public void Change (String str, char ch[]) {
str = "Hello";
Ch[0] = ' W ';
}
}
~~~
The result is Hello world.
The str inside the change method is a new space to point you to Hello, but the test is immutable.
Summarize:
1, master the construction method of overloading
2. Some common types of string
Java Second Job