Java_object_0070.java

目次

クラスの使い方 Java

インスタンス化

ソースコード

class Cat {
  public String name = "Human" ;
  public int cWalk = 20 ;
  public String sWalk = "." ;

  public Cat(String n ) {
      this.name = n;
      this.cWalk = 20;
      this.sWalk = ".";
  }

  public void Walk() { 
      String s = "";
      for(int i = 1; i < this.cWalk; i++ ) {
        s += this.sWalk ;
      }
      System.out.print("Walk\t" ) ;
      System.out.println( s + "(" + this.name + ")" );
    }
}

class subMain {
  public subMain(){   
    Cat pochi = new Cat("pochi");
    Cat tama  = new Cat("tama");
    Cat tora  = new Cat("tora");
    Cat buchi = new Cat("buchi");
    
    pochi.Walk();

    tama.cWalk = 10;
    tama.Walk();
  
    buchi.name = "shiro"; 
    tora.Walk();

    buchi.Walk();
  }
}

public class Java_object_0070 {
  public static void main ( String[] args ) {
    subMain root = new subMain() ;
  }
}
よかったらシェアしてね!
目次