Object-oriented basic 02 implementation goals
Use of the 1.String class
2. Master the use of this keyword
3. Mastering the use of the static keyword
String class
Instantiating a String object
A string is an anonymous object of a string class, an object that has already opened up a heap of memory space and can be used directly.
1. Use the direct assignment method to operate.
String name = "zhangsan";
In this way, if a string is already referenced by a name, then the same string declaration will no longer open up memory space.
2. Call the constructor of the string class directly (public string orignal).
String name = new String("zhangsan")
Using this method can result in a waste of memory. Because a string is an anonymous object of a string class, using the New keyword opens up a new memory space.
String Content Comparison
1. "= =" (address value comparison) [STR1 = = str2]
2.equals () (content comparison) [Str1.equals (STR2)]
The contents of a string cannot change the contents of a string once the declaration is immutable, a string content can be changed actually through the memory address of the "disconnection-connection" change, and the contents of the string itself does not change.
Common methods of the string class
The string class provides a number of methods of operation, commonly used as follows:
方法 效果s.length() 返回s字符串长度 s.charAt(3) 返回s字符串中下标为3的字符 s.substring(0, 6) 返回s字符串中下标0到6的子字符串 s.indexOf("Hello") 返回子字符串"Hello"的下标s.startsWith(" ") 判断s是否以空格开始 s.endsWith("hh") 判断s是否以"hh"结束s.equals("Good World!") 判断s是否等于"Good World!" ==只能判断字符串是否保存在同一位置。 需要使用equals()判断字符串的内容是否相同。 s.compareTo("Hello World!") 比较s字符串与"Hello World!"在词典中的顺序, 返回一个整数,如果<0,说明s在"Hello World!"之前; 如果>0,说明s在"Hello World!"之后; 如果==0,说明s与"Hello World!"相等。s.trim() 去掉s前后的空格字符串,并返回新的字符串s.toUpperCase() 将s转换为大写字母,并返回新的字符串 s.toLowerCase() 将s转换为小写,并返回新的字符串 s.replace("World", "Universe") 将"World"替换为"Universe",并返回新的字符串
this keyword
Usage:
1.this emphasizes methods of this class
2. Represents a property in a class
3. You can use this to call the constructor of this class (which must be placed in the first row of the construction method)
This calls the constructor method when you must leave a constructor as the exit, That is, there is at least one constructor method in the program that does not use this to invoke other constructors.
4.this represents the current object
/***1. Define a student class and test the class. Completes the comparison of two objects, when comparing, the first address comparison, if the address of two objects is the same, * is certainly the same object, and if the address is not equal, then the property will be compared in turn. */class student{private String stuid; Study number private String name; Name Public Student () {} public Student (String stuid,string name) {this.setstuid (STUID); This.setname (name); }//When calling this method, there are two objects: the current object, the incoming object public boolean compare (Student stu) {Student stu1 = this; An object that represents the currently calling method Student Stu2 = stu; The object passed to the method if (stu1 = = STU2) {//Compares two memory addresses to return true; } if (Stu1.stuid.equals (stu2.stuid) &&stu1.name==stu2.name) {//compares the equality of each property to return true; }else{return false; }} public void Setstuid (String stuid) {this.stuid = Stuid; } public String Getstuid () {return this.stuid; } public void SetName (String name) {this.name = name; } Public String GetName () {return this.name; }}/***1. Write the test class, test the above code */class demo02{public static void Main (String [] args) {Student stu1 = new Student ("1114020 116 "," Zhang San "); Student STU2 = new Student ("1114020116", "Zhang San"); if (Stu1.compare (STU2)) {System.out.println ("Two objects are equal! "); }else{System.out.println ("Two objects are not equal! "); } }}
Static keyword
This variable is called a static variable if the variable is declared with static in the program. If many objects have a common property and the value is the same, you can use static to decorate the property. Because the properties of the static declaration are shared by all objects.
/***1. Define a Student class and test the class */class student{private String stuid; Study number private String name; Name static String city = "China"; Using the static Definition property, there is a default value of public Student () {} public Student (String stuid,string name) {this.setstuid (STUID); This.setname (name); } public void Setstuid (String stuid) {this.stuid = Stuid; } public String Getstuid () {return this.stuid; } public void SetName (String name) {this.name = name; } public String GetName () {return this.name; The public String tell () {return "I call:" +this.name+ "study number is:" +this.stuid+ "is currently in" +city; }}/***1. Write the test class, test the above code */class demo03{public static void Main (String [] args) {Student stu1 = new Student ("1114020 116 "," Zhang San "); Student STU2 = new Student ("1114020117", "John Doe"); Student stu3 = new Student ("1114020118", "Harry"); System.out.println ("Before modifying information:" +stu1.tell ()); System.out.println ("Before modifying information:" +stu2.tell ()); System.ouT.println ("Before modifying information:" +stu3.tell ()); System.out.println ("--------------------------"); Student.city = "United States"; Modify the static property System.out.println (after "Modify Information:" +stu1.tell ()); System.out.println ("After modifying information:" +stu2.tell ()); System.out.println ("After modifying information:" +stu3.tell ()); }}
Java Basic Learning 05 (Object-oriented foundation)