我学继承时出现的问题,到底该怎么继承啊?我写好了一个类。现在想用另一个类进行继承,我不知道那个子类和超类该怎么放要是我想引用一个已经写好了的文件里的类。应该怎么写呢?
两个类如下
public class Domineering {

    public static final java.util.Scanner INPUT = new java.util.Scanner(System.in);
    
    public static final boolean HORIZONTAL = false;
    
    public static final boolean VERTICAL = true;
  
private boolean [][] squares;
    public Domineering(int input) {
     squares = new boolean[input][input];
     for(int row = 0; row < squares.length ; row ++){
     for(int column = 0; column < squares.length ; column++){
     squares[row][column] = false;
     }
     }
    }
    
    public boolean hasLegalMoveFor(boolean player) {
     int rowOffset = 0;
     int columnOffset = 0;
     if (player == HORIZONTAL) {
     columnOffset = 1;
     } else {
     rowOffset = 1 ;
     }
     for (int row = 0; row < (squares.length - rowOffset); row ++) {
     for (int column = 0; column < (squares.length - columnOffset); column ++) {
     if(!(squares[row][column]||squares[row+rowOffset][column+columnOffset])){
     return true;
     }
     }
     }
     return false;
    }
    
    public boolean isOkey(int row , int column , boolean player){
     if (player == HORIZONTAL) {
     return row>=0 && row<squares.length && column>=0 && column<squares.length-1 && !squares[row][column] && !squares[row][column+1] ;
     } else {
     return row>=0 && row<squares.length-1 && column>=0 && column<squares.length && !squares[row][column] && !squares[row+1][column] ;
     }
    }
    
    public void playAt(int row , int column , boolean player){
     squares[row][column] = true;
     if(player ==  HORIZONTAL){
     squares[row][column+1] = true;
     } else {
     squares[row+1][column] = true;
     }
    }
    
    public void play(){
     boolean player = HORIZONTAL ;
     while (true) {
     System.out.println ("\n" + this);
     if (player == HORIZONTAL) {
     System.out.println ("Horizontal to play") ;
     } else {
     System.out.println ("Vertical to play") ;
     }
     if (!(hasLegalMoveFor(player))){
     System.out.println ("No legal moves -- you lose") ;
     return ;
     }
     System.out.print ("Row: ");
     int row = INPUT. nextInt();
     System.out.print ("Column: ");
     int column = INPUT.nextInt();
     while(!(isOkey(row,column,player))) {
     System.out.println ("Your input is wrong, please input again.");
     System.out.print ("Row: ");
     row = INPUT.nextInt();
     System.out.print ("Column: ");
     column = INPUT.nextInt();
     }
     playAt(row, column, player);
     player = !player;
     }
    }
    
    public String toString() {
     String result = "  " ;
     for (int row = 0 ; row < squares.length ; row ++){
     if (row < 10){
     result = result + "  " +row;
     } else {
     result = result + " " + row;
     }    
     }
     for(int row = 0 ; row < squares.length ; row ++){
     if(row < 10){
      result += "\n " + row;
     } else {
     result += "\n" + row ;    
     }
     for (int column = 0 ; column < squares.length ; column++){
     if (squares[row][column]){
     result += "  #" ;
     } else {
     result += "  ." ;
     }
     }
     }
     return result ;
    }
        
    public static void main (String[] args) {
     System.out.println ("Welcome to DomineeringGame!");
     System.out.print ("Please input the sizes of squares: ");
     int input = INPUT.nextInt();
     Domineering game = new Domineering(input);
     game.play();
    }
    
}//*****************************************************************************************public class Cram extends Domineering {    public Cram() {
     super();
    }
    
    public void play() {
     int player = 1;
     while (true) {
     System.out.println ("\n" + this);
     System.out.println ("Player "+ player + " to play");
     if(!(hasLegalMoveFor(HORIZONTAL)||hasLegalMoverFor (VERTICAL))){
     System.out.println ("No Legal moves -- you lose ! ");
     return ;
     }
     System.out.print ("Row: ");
     int row = INPUT.nextInt();
     System.out.print ("Column: ");
     int column = INPUT.nextInt();
     INPUT.nextLine();   //To clear out input ;
     System.out.print ("Play horizontally (y/n)?");
     boolean direction ;
     if(INPUT.nextLine().charAt(0)== 'y') {
     direction =HORIZONTAL;
     }else {
     direction = VERTICAL;
     }
     playAt(row, column , direction);
     player=3- player;   
     }
    }
    
    public static void main (String[] args) {
     System.out.println ("Welcome to CramGame!");
     Cram game = new Cram();
     game.play();
    }
    
    
}

解决方案 »

  1.   


    public class Cram extends Domineering {
    ...不明白你要表达什么意思
      

  2.   

    确定这是学继承的时候?
    public class Person{
      String name;
    }public class Student{
      public static void main(String [] args){
         name = "LZ";
      }
    }
      

  3.   

    好吧 虽然继承就是一个extends就能搞定,但是我还是想把我的理解在表述一下:继承就是为了代码复用
    比如说有下面三个类:
    public class Persion {
    // 声明一个name属性
    public String name = ""; public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    // 打印name属性
    public void print() {
    System.out.println("I am " + this.getName());
    }
    }
    public class Adult extends Persion {
    // 什么都没有
    }
    public class Baby extends Persion {
    // 什么都没有
    }一个Person类和Person类的两个子类Adult和Baby
    Adult和Baby在继承了Person类后,虽然在自己的类里面什么代码都没有写,确可以直接调用Person类的方法
    public class Test {
    public static void main(String[] args) {
    // 声明一个成人类
    Adult a = new Adult();
    // 声明一个婴儿类
    Baby b = new Baby();
    // 为成人类设置name属性
    a.setName("Adult");
    // 为婴儿类设置name属性
    b.setName("Baby");
    a.print();
    b.print();
    }
    }
    超类和子类摆放的位置不用担心,可以放在同一个包下,也可以放在不同的包下
      

  4.   

    本人第一次在这里发帖我想顺便问一下在这里要让代码好看点前后是加什么?
    有的网站是:<code>和<code/>
    这里是怎么样的啊?
      

  5.   


    很感谢你的回答但我现也是将我帖中的两个类放在同一个文件 里了但就是编译出错
    如你所说,子类和超类可以随便放那能不能你把在一个文件 和不在一个文件里的两种情况都举
    一个例子给我,最好写上文件的名,我现在最迷惑的是两个类放在一个文件的话那文件名用哪个呢?
    两个类里都有main的函数,我的要求是对原来那个文件不做任何改变!谢谢了!!!
      

  6.   

    一个文件只能有一个public的类
    引用其他文件的类没什么特别要求,直接用就好(注意类包)
      

  7.   

    现在eclipse写了  在copy上来...就有颜色,而且格式也很规范.在eclipse中使用shift,ctrl+f来格式化.十分常用的组合键~~~
      

  8.   

    <java code>   public boolean hasLegalMoveFor(boolean player) { 
        int rowOffset = 0; 
        int columnOffset = 0; 
        if (player == HORIZONTAL) { 
        columnOffset = 1; 
        } else { 
        rowOffset = 1 ; 
        } 
        for (int row = 0; row < (squares.length - rowOffset); row ++) { 
        for (int column = 0; column < (squares.length - columnOffset); column ++) { 
        if(!(squares[row][column]||squares[row+rowOffset][column+columnOffset])){ 
        return true; 
        } 
        } 
        } 
        return false; 
        } <java code/> 
      

  9.   


    测试: 
    看下是不是加  java code 和 java code/<java code> public static void main <java code/>
      

  10.   


    难到只有用eclipse写的代码才能这样好看没有别的方法了吗?我是用JCreater 写的那该怎么办啊?
      

  11.   

    不好意思 没有及时给楼主回帖-_-!
    希望楼主能看我的这层回帖
    首先,有什么来写(eclipse还是JCreater)并不重要,在回帖编辑区的上方,有一条工具按钮,就是A、B、I、US……
     在下划线A的右边有一个带井字的图标,点击就会出现一个下拉框,再点下JAVA就会在编辑区出现如下的标签:
     "【code=Java】【/code】"
     在标签内写上代码就可以自动格式化了
    第二,我说的"子类和超类可以随便放",是说可以放在不同的包里,不是指的一个文件内,也可以放在一个文件内,但一个JAVA文件内,
    只能有一个public修饰符的类,否则编译的时候会出错,解决办法就是把文件内其他类的public修饰符去掉,还要注意的是,文件名要和public修饰符修饰的类同名如果还有问题的话,楼主直接给我发消息好了:)