java
  • string
  • object
  • constructor
  • add
  • 2016-10-15 3 views 0 likes 
    0

    저는 Java를 처음 사용합니다. 어떻게 문자열 데이터를 객체 "myData"에 추가하고 메인에 그 내용을 출력합니까?Java : 객체에 데이터를 어떻게 추가합니까?

    @Override 
    public String toString() { 
        return "myData{" + 
         "fullName=" + Arrays.toString(fullName) + 
         ", skype='" + skype + '\'' + 
         ", github='" + github + '\'' + 
         ", myData=" + myData + 
         '}'; 
    } 
    

    :이 같은 예를 들어, 방법 toString()을 무시할 수있는 내용을 인쇄하려면

    public static void main(String[] args) { 
        myData myData = new myData(myArray, "skype", "github"); 
        ... 
    } 
    

    :

    public class myData { 
        static String[] myArray = new String[] { "Mimi Rudolph", "minirudolph" }; 
    
        public static String[] cutName(String string) { 
        return string.split(" "); 
        } 
    
        String[] fullName = cutName(myArray[0]); 
        String skype = myArray[1]; 
        String github = null; 
        Object myData = new Object(); 
    
        public myData(String[] fullName, String skype, String github) { 
        this.fullName = fullName; 
        this.skype = skype; 
        this.github = github; 
        } 
    
        public static void main(String[] args) { 
    
        Object myData = new Object(); 
        } 
    } 
    

    답변

    1

    @Tai, 나는 당신이 몇 가지 개념을 놓치고있어 생각하여 암호. Java가 Object Oriented Programming 언어 인 경우 클래스의 새 인스턴스를 만들고자 할 때 메서드에서 static을 사용하지 않아야합니다.

    static와 방법을 호출하려면, 당신은 당신이 새로운 인스턴스를 만들 때, 생성자에 액세스 할 것 (예를 들어, MyData.cutName로 호출 할 수 있습니다. 반면에

    을 객체의 새로운 인스턴스를 필요가 없습니다 개체의.

    난 당신이 배열을 제거 할 수 생각하지만, 나는 당신의 cutName 방법을 지켰다. 당신이 뭔가를 가질 수있다.

    public class MyData { 
        private String fullname; 
        private String skype; 
        private String github; 
    
        public MyData(String fullname, String skype, String github) { 
         this.fullname = fullname; 
         this.skype = skype; 
         this.github = github; 
        } 
    
        public String getFullname() { 
         return this.fullname; 
        } 
    
        public String getSkype() { 
         return this.skype; 
        } 
    
        public String getGithub() { 
         return this.github; 
        } 
    
        public String[] cutName(String string) { 
         return string.split(" "); 
        } 
    
        @Override 
        public String toString() { 
         return "Fullname: " + this.fullname + "; Skype: " + this.skype + "; Github: " + this.github; 
        } 
    
        public static void main(String[] args) { 
         MyData myData = new MyData("Mimi Rudolph", "minirudolph_skype", "minirudolph_githnub"); 
         System.out.println("First name: " + myData.cutName(myData.getFullname())[0]); 
         System.out.println("Last name: " + myData.cutName(myData.getFullname())[1]); 
         System.out.println(myData); 
        } 
    } 
    

    을 출력은

    First name: Mimi 
    Last name: Rudolph 
    Fullname: Mimi Rudolph; Skype: minirudolph_skype; Github: minirudolph_githnub 
    

    클래스에 속성을 설정하고 새 인스턴스에서 설정하면 재사용이 가능합니다.

    희망이 있습니다.

    +0

    고마워요! 그게 내가 원하는거야! :) – Tai

    1

    당신은 단순히 myData로 옆 클래스의 인스턴스를 생성해야 그러면 System.out.println(myData)을 사용하여 콘텐츠를 인쇄 할 수 있습니다.

    그래서 최종 코드는 다음과 같습니다

    public class myData { 
        ... 
        @Override 
        public String toString() { 
         ... 
        } 
    
        public static void main(String[] args) { 
         myData myData = ... 
         System.out.println(myData); 
        } 
    } 
    
    1

    을 당신이 이런 식으로 뭔가를 찾고 생각 :

    public class MyData { 
    
    private static final String[] myArray = new String[]{"Mimi Rudolph", "minirudolph"}; 
    
    String[] fullName = cutName(myArray[0]); 
    String skype = myArray[1]; 
    String github = null; 
    Object myData = new Object(); 
    
    private static String[] cutName(String string) { 
        return string.split(" "); 
    } 
    
    public MyData(String[] fullName, String skype, String github) { 
        this.fullName = fullName; 
        this.skype = skype; 
        this.github = github; 
    } 
    
    public static void main(String[] args) { 
        MyData myData = new MyData(myArray, "Skype string goes here", "githun string goes here"); 
        System.out.println(myData.fullName); 
        System.out.println(myData.github); 
        System.out.println(myData.skype); 
    } 
    } 
    
    1

    일반적으로 다음과 같은 간단한 예에 의해 객체에 문자열을 추가 할 수 있습니다

    Object[] myData = new Object[myArray.length]; 
         for(int i=0;i<myArray.length;i++){ 
          myData [i] = myArray[i]; 
          System.out.println("MyData Object Array holding strings data: "+myData[i]); 
         } 
    
    :

    String a = "abc"; 
    Object b = a; 
    System.out.println(b); 
    

    당신이 MYDATA 개체에 대한 완전한 문자열 배열을 할당 할 경우, 다음을 수행해야 당신의 주요 방법에서

    은, 먼저 배열의 객체에 객체를 변경해야 에서 - Object myData = new Object(); TO : Object[] myData = new Object[myArray.length];

    관련 문제