Java Second Job

Source: Internet
Author: User

(a) Learning summary
1. What is a construction method? What is an overload of a constructor method? Can the following program be compiled? Why?

public class Test {    public static void main(String args[]) {        Foo obj = new Foo();           }     }class Foo{    int value;    public Foo(int intValue){        value = intValue;    }}

A constructor method is a special method, which is a method with the same name as the class and a return value type of the class type with the same name. The creation of an object is accomplished by means of a construction method, whose function is to initialize the object.
Construction method overloading is the same method name, by passing the number of parameters and different types to complete the invocation of the method
This program cannot be compiled because there is no empty construction method in this program, and the compiler does not define a default parameterless construction method after the class has a parameter constructor defined.
2. What is the result of running the following procedure? Analyze the reason and how it should be modified.

public class Test {    public static void main(String[] args) {        MyClass[] arr=new MyClass[3];        arr[1].value=100;    }}class MyClass{    public int value=1;}


Not instantiated for Arr[1]
3. What is the result of running the following procedure? explain why.

public class 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 false
The reason is that after instantiating an object, the system assigns an address to the object, and the two object addresses are unequal.
4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
Encapsulation is the combination of the object's member properties and member methods into a separate unit, and as far as possible to conceal the inner details of the object, which contains two meanings.
⑴ combines all member properties of an object with all member methods to form an indivisible independent unit
⑵ information concealment, that is, as far as possible to conceal the internal details of the object, the external formation of a boundary, only a limited external interface refers to the external connection.
In defining a member of a class (including variables and methods), the private keyword is used to describe the access rights of the member, which can only be called by other member methods of the class, not by methods in other classes; For encapsulation, the member variable of the class is declared as private. This variable is then accessed through the public method. The operation of a variable, generally have read and assignment operation, we generally define two methods to achieve these two operations, namely: GetXxx () and setxxx ();
5. Read the procedure below to see if you can compile the pass? If not, explain why.
(1)

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 be compiled because private objects that have private as a class before secret cannot be called in other classes
(2)

public class Test{    int x = 50;    static int y = 200;    public static void method(){        System.out.println(x+y);    }    public static void main(String args[]){        Test.method();    }}

cannot be compiled; X is not static cannot be called in Y
6. Use static variables and construction methods of a class to 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. ‘’

public Book(String bookName, double price) {    i++;    this.bookName = bookName;    this.price = price;    bookId = i;}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;}// 定义方法求图书总册数public static int totalBook() // 定义方法求图书总册数{    return i - 1000;}// 重写toString方法public String toString() // 重写toString方法{    return "图书名称:" + this.getBookName() + "\n" + "图书价格:" + this.getPrice() + "\n" + "图书编号:" + bookId + "\n";

7. What is a singleton design pattern? What characteristics does it have? Design a solar class sun with a single-instance design pattern.
The singleton mode ensures that a class has only one instance, provides this instance on its own and provides this instance to the system as a whole.
Features: 1. A class can have only one instance of 2. Create this instance yourself 3. This instance is used throughout the system

public class sun {        private sun(){}     private static sun num=new sun();     public static sun getNum(){            return num;        }}

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("你好  ");    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‘;    }}

Result: Hello Wolld

Java Second Job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.