Java Technology Second Job

Source: Internet
Author: User

(a) Study summary 1. What is a construction method? What is an overload of a constructor method?

The constructor method is used to initialize the object, and the constructor is called when the new object is created.
Each class has a constructor function. The Java compiler creates a default constructor for this class in cases where the programmer does not provide a constructor for the class.
The overloading of constructors is that the function name is the same as the class name, with different parameter types and different parameters.
1. The name of the construction method must be the same as the class name
2. The declaration of a constructor method cannot have any declaration of a return value type
3. Cannot use return in construction method
4. When you declare a constructor, the system no longer creates a default parameterless construction 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;    }}

cannot be compiled because there is no parameterless construction method, only the parameter constructor method is defined in the class, at which point the compiler no longer establishes the default parameterless construction method.

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;}

Compile an error, declare only an array of objects, do not implement object instantiation, should be in the array of each object element, through the new construction method to instantiate

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;}

False, because the reference address of two objects is compared, and two objects are different new spaces opened by new.

4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
  1. Not all information in a class is visible to the user, so it is necessary to hide the information. This is the object-oriented requirements for encapsulation.
  2. The purpose of encapsulation: to protect certain properties and methods of the current class from being externally visible.
  3. Encapsulation implementation: The encapsulation of Java is implemented by declaring properties and methods with the private modifier.
    Scope Current class same Bun class other package
    Public√√√√
    Protected√√√
    Default√√
    Private√
  4. The Get/set method that implements the property is accessed externally.
  5. The encapsulated property cannot be accessed by the object. Properties as before. The corresponding Get/set method is called through the object.
    For example:

    public class Time {private int year,month,day; //字段用private隐藏起来public Time(){}public Time(int year,int month,int day){    this.year=year;    this.month=month;    this.day=day;}public int getYear() {  //通过get,set方法读取隐藏数据    return year;}public void setYear(int year) {    this.year = year;}public int getMonth() {    return month;}public void setMonth(int month) {    this.month = month;}public int getDay() {    return day;}public void setDay(int day) {    this.day = day;}public String toString( ){    return year+"-"+month+"-"+day;}}
    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++);}}

    No, because the secret field is private, you cannot access the operation directly.

    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();}}

    The Mothod method is static decorated and can only access static decorated fields, X is not.

    6. Use static variables and construction methods of a class to track the number of objects created by a class. Declare a book class, data members are numbered,

    Title, book price, and the total number of static data member books recorded. The book number starts with 1000, each producing an object,
    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.

class book{int bookId;    String BookName;    Double Price;        Declare static variable public static n;    Defines static code blocks for static variables initialized with the static {n=0;        }//Construction method public book (String bookname,double price) {this.bookname=bookname;        This.price =price;        n++;    This.bookid=1000+n;    } 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;     }//define method to find the total number of books public int Totalbook () {return n;    }//Override toString method public string ToString () {return "number" +bookld+ "title" +bookname+ "Price" + "prices" + "total books" +n;                        }}public class test{public static void Main (String args[]) {book[] books = {new book ("C Language Programming", 29.3),        New book ("Database Principle", "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.

Singleton mode: Ensure that a class is in use with only one instance.
Features: This class always has only one instance.

class Sun{      private Sun instance =  new Sun();      private Sun(){       }      public static Sun getInstance(){       return instance;      }}
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
STR initially copied its own reference to the local STR, but the local str also refers to Hello, when the function ends the call, the local STR released, does not involve far STR changes;
The character array and the C principle are the same, through the incoming address, directly on the address modification;

(ii) experimental Summary (iii) code hosting (be sure to link to your project)


Https://gitee.com/hebau_java_cs16/Java_CS02tx

Java Technology 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.