1 //Examples of immutable classes:2 /*3 The following program attempts to define an immutable class person class, but = because the person class contains a member variable of a reference type,4 and this reference class is a mutable class, so the person class becomes a mutable class. 5 */6 className17 {8 PrivateString FirstName;9 PrivateString LastName;Ten PublicName1 () {} One PublicName1 (String firstname,string lastName) A { - This. FirstName =FirstName; - This. LastName =LastName; the } - Public voidSetfirstname (String firstName) { This. FirstName =FirstName;} - PublicString Getfirstname () {returnFirstName;} - Public voidSetlastname (String lastName) { This. LastName =LastName;} + PublicString Getlastname () {returnLastName;} - } + Public class Person A { at Private FinalName1 name;//reference type member variable for person class - PublicPerson (Name1 name) - { - This. Name =name; - } - PublicName1 getName () in { - returnname; to } + Public Static voidMain (string[] args) - { theName1 n =NewName1 ("Goku", "Sun")); *Person p =NewPerson (n); $ //The firstname value for the name of the person object is "Goku"Panax Notoginseng System.out.println (P.getname (). Getfirstname ()); - System.out.println (P.getname (). Getlastname ()); the //change the name of the person object to the FirstName value of "Eight commandments" +N.setfirstname ("Eight commandments"); A System.out.println (); the //The firstname value of the name of the person object is changed to "eight commandments" + System.out.println (P.getname (). Getfirstname ()); - System.out.println (P.getname (). Getlastname ()); $ } $}
1 //Examples of immutable classes:2 /*3 The following program attempts to define an immutable class person class, but = because the person class contains a member variable of a reference type,4 and this reference class is a mutable class, so the person class becomes a mutable class. (This example protects the person object's5 member variable of reference type: name to make the person class A true invariant Class)6 */7 className18 {9 PrivateString FirstName;Ten PrivateString LastName; One PublicName1 () {} A PublicName1 (String firstname,string lastName) - { - This. FirstName =FirstName; the This. LastName =LastName; - } - Public voidSetfirstname (String firstName) { This. FirstName =FirstName;} - PublicString Getfirstname () {returnFirstName;} + Public voidSetlastname (String lastName) { This. LastName =LastName;} - PublicString Getlastname () {returnLastName;} + } A Public classPerson1 at { - Private FinalName1 name;//reference type member variable for person class - PublicPerson1 (Name1 name) - { - //sets the name instance variable to a temporarily created name object, the object's FirstName and LastName - //Same as FirstName and LastName of the name parameter passed in in This. Name =NewName1 (Name.getfirstname (), Name.getlastname ()); - } to PublicName1 getName () + { - //returns an anonymous object with the FirstName and LastName of the object. the //same as the FirstName and LastName of the name of the object. * return NewName1 (Name.getfirstname (), Name.getlastname ()); $ }Panax Notoginseng Public Static voidMain (string[] args) - { theName1 n =NewName1 ("Goku", "Sun")); +Person1 p =NewPerson1 (n); A //The firstname value for the name of the person object is "Goku" the System.out.println (P.getname (). Getfirstname ()); + System.out.println (P.getname (). Getlastname ()); - //change the name of the person object to the FirstName value of "Eight commandments" $N.setfirstname ("Eight commandments"); $ System.out.println (); - //The firstname value of the name of the person object is changed to "eight commandments" - System.out.println (P.getname (). Getfirstname ()); the System.out.println (P.getname (). Getlastname ()); - }Wuyi}
1 /*2 Immutable (Immutable) class: the instance variable of the instance is immutable after the loss of the class is created. 3 the 8 wrapper classes and Java.lang.String classes provided by Java are immutable classes, and their instance variables cannot be changed when their instances are created. 4 5 */6 7 //test the Equals () method and the Hashcode method of the Java.lang.String class ()8 Public classimmutablestringtest9 {Ten Public Static voidMain (string[] args) One { AString str1 =NewString ("Hello"); -String str2 =NewString ("Hello"); -System.out.println (str1 = =str2); the System.out.println (Str1.equals (str2)); - //The following two output hashcode are equal - System.out.println (Str1.hashcode ()); - System.out.println (Str2.hashcode ()); + } -}
Immutable classes in Java