μμ±μ λ€μ€ μ μ
- μμ±μλ λ€μ€ μ μν μ μμ΅λλ€.
- μμ±μκ° λ€μ€ μ μλμλ€κ³ ν μ§λΌλ, νΈμΆλλ μμ±μλ 1κ°μ λλ€.
class MyString {
private byte[] string;
public MyString() {}
public MyString(String param) {
this.string = param.getBytes();
}
public MyString(int param) {
String tmp;
tmp = String.format("%d", param);
this.string = tmp.getBytes();
}
public String getString() {
if (string == null || string.length == 0) return "(null)";
return new String(string);
}
}
public class Main {
public static void main(String[] args) {
MyString str1 = new MyString("Hello");
MyString str2 = new MyString("123");
System.out.println(str1.getString());
System.out.println(str2.getString());
}
}
μμ 볡μ¬
μΈμ€ν΄μ€κ° λμ΄λλ κ²μ΄ μλλΌ λ¨μν μ°Έμ‘°μλ§ λμ΄λλ κ²½μ°μ λλ€. μ€λ₯κ° μΌμ΄λ κ°λ₯μ±μ΄ ν½λλ€.
κΉμ 볡μ¬
μλ³Έ μ체λ μλ‘ ν λΉνκ³ , 볡μ¬νλ λ°©μμ
λλ€. μ€μ λ‘ μΈμ€ν΄μ€λ₯Ό λ리λ κ²μ
λλ€.
clone()μ μ¬μ©νμ¬ λ³΅μ¬λ μΈμ€ν΄μ€λ₯Ό λ§λλ λ°©λ²μ΄ μμΌλ, κ·μ½μ΄ λͺ¨νΈνκ³ , μμΈμ²λ¦¬κ° 볡μ‘νλ€λΌλ λ¨μ μ΄ μμ΅λλ€.
λ³΅μ¬ μμ±μλ₯Ό μ΄μ©νλ©΄ μ΄ λ¨μ μ μμ¨ μ μμ΅λλ€.
μ°Έμ‘°μμ μΈμ€ν΄μ€λ₯Ό ꡬλ³νλκ² λ§€μ° μ€μν©λλ€!
μμ
class MyString {
private byte[] string;
MyString() {
string = null;
}
public MyString(String param) {
string = param.getBytes();
}
public MyString(MyString rhs) {
this.deepCopy(rhs);
}
public void deepCopy(MyString rhs) {
this.string = rhs.string.clone();
}
public String getString() {
if(string == null || string.length <= 0) {
return "(null)";
}
return new String(string);
}
public void setString(String param) {
string = param.getBytes().clone();
}
}
public class Main {
public static void main(String[] args) {
MyString src = new MyString("Hello");
MyString dst = new MyString(src);
src.setString("World");
System.out.println(dst.getString());
}
}
μ μ λ©€λ² (static)
- μΈμ€ν΄μ€κ° μμ΄λ νΈμΆμ΄ κ°λ₯ν λ©€λ²(νλ, λ©μλ)μ λλ€.
- thisλ μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν€λ κ²μ΄κ³ , staticμ μΈμ€ν΄μ€ λ©λͺ¨λ¦¬λ₯Ό κ°μ΄ μ°μ§ μκΈ° λλ¬Έμ, thisλ‘ μ μ λ©€λ²μ μ κ·Όν μ μμ΅λλ€.
- finalκ³Ό ν¨κ» μ μΈν¨μΌλ‘μ¨ μ¬λ³Όλ¦ μμλ‘ μ¬μ©νλ κ²½μ°κ° λ§μ΅λλ€.
μ μ λ©μλλ‘ MyString ν΄λμ€ μμ±νκΈ°
class MyString {
private byte[] string;
private static int count;
private MyString() {
string = null;
}
private MyString(int param) {
this.string = String.format("%d", param).getBytes();
}
private MyString(String param) {
this.string = param.getBytes();
}
public String getString() {
if(string == null || string.length <= 0) {
return "(null)";
}
return new String(string);
}
public static MyString newString(int param) {
count++;
return new MyString(param);
}
public static MyString newString(String param) {
count++;
if(param == null || param.length() == 0) {
return new MyString("(null)");
}
return new MyString(param);
}
public static int getCount() {
return MyString.count;
}
}
public class Main {
public static void main(String[] args) {
// MyString src = new MyString("Hello");
MyString s1 = MyString.newString(10);
MyString s2 = MyString.newString("Test");
System.out.println(s1.getString());
System.out.println(s2.getString());
System.out.println(MyString.getCount());
}
}
μΆμ²
'π¨βπ» Programming > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] ν΄λμ€ (1) (0) | 2024.12.31 |
---|---|
[Java] String κ°μ²΄κ° λΉμλμ§ νμΈνλ λ°©λ² (0) | 2024.10.27 |