(a) Study summary 1. What is a construction method? What is an overload of a constructor method? Can the following program be compiled? Why?
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. Constructor methods are called automatically when a class instantiates an object. The construction method is used to initialize an object when it is created.
Constructor overloading and method overloading are similar in Java. You can create multiple constructors for a class. Each constructor must have its own unique argument list. Even though the name of the method is the same, the number of incoming types of the arguments is different, but the names are the same.
~ Publicclass 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 no arguments are defined, and only constructors with parameters are defined, and no arguments are created.
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;}~~~运行结果:Exception in thread "main" java.lang.NullPointerException只声明的对象数组,没有实现对象实例化,应对数组里的每个对象元素,然后通过new构造方法进行实例化。###3.运行下列程序,结果是什么?说明原因。
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
Obj1 Obj2 is the opening of two new address addresses that are different so for false.
4. What is object-oriented encapsulation and how is encapsulation implemented in Java?
Encapsulation is one of the three main features of object-oriented programming, which is to combine the object's member properties and member methods into a separate unit, and to conceal the inner details of the object as much as possible, including the following two meanings.
(1) Combine all member properties of an object with all member methods to form an indivisible independent unit (i.e. object)
(2) 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.
~~~
Private String classs;
Public String getclasss () {
return classs;
}
public void Setclasss (String N) {
Class=n;
}
Public String toString () {
Return "class:" +getclasss ();
public class text{
public static void Main (String args[]) {
Person Per=new person ();
Per.setclasss ("First Class");
SYSTEM.OUT.PRINTFLN (per.tostring);
}
}~~~
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 = new A ();
System.out.println (a.secret++);
}
}~
The compilation cannot be passed because secret is defined as private and other inaccessible.
(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 ();
}
}~~~
Compilation does not pass, and x is defined as static as 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.
~~~class book{
int bookId;
String BookName;
Double Price;
declaring static variables
//定义静态代码块对静态变量初始化//构造方法 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;} //定义方法求图书总册数//重写toString方法
}
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.
Singleton mode: is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that the class with the pattern can have only one instance of the class that is used in the system. That is, a class has only one instance of an object
Characteristics:
(1) Instance control: Singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances.
(2) Flexibility: Because classes control the instantiation process, classes can flexibly change the instantiation process.
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 ';
}
}~~~
Running Result: Hello Wolld
The contents of the original STR do not change,
(ii) Experimental summary
Email verification
(1) In the keyboard input to verify the email, first query whether there is "@" and "." And the former to be in front of the latter.
(2) Decide whether to start with "@".
(3) Look at the end is not with com,cn,net,gov,edu,org end.
Find string
(1) record the input string and the string to be queried
(2) Find the position of the first searched string, plus the value of the lookup string to the location in the public int indexOf (string str, int fromIndex). Every time. Find the address plus the length of the lookup string returned to IndexOf (string str, int fromIndex)
(3) Then output
Statistics file
(1) Enter the name of the file to be categorized
(2) separate the input string into a string array using the Split method.
(3) Divide each string in the string array into a string array, then capitalize the first of each string array and force a string array back
(4) The end of the string is pressed "." Once again, the new string array is compared from the first and then the same as null and the number of records returns the output.
(c) Code hosting
Https://gitee.com/hebau_java_cs16/Java_CS01WangTianLun
Java Second Experiment