наследование классов Java - PullRequest
       4

наследование классов Java

0 голосов
/ 02 февраля 2012

У меня есть основной класс, в который пользователь вводит данные, после того как я хотел бы перейти в другой класс. Я не знаю, как перейти к следующему классу и наследовать значения x и y в коде Java.

public class main {
                     int x=25;
                     int y =25;
                   //Go to next class second
                  }

public class second {
                     //inherit values of x and y
                     //manipulate values 
                     //Go to next class third
                    }

public class third {
                     //inherit values of x and y from second class
                     //manipulate values 
                   }

Ответы [ 5 ]

3 голосов
/ 02 февраля 2012

Описанная вами проблема не имеет ничего общего с наследованием. Решение представляется двухэтапным процессом.

  1. Изучение Java .
  2. Передать нужные значения в качестве параметров вызовам метода.
2 голосов
/ 02 февраля 2012

Я не думаю, что вы имеете в виду наследование здесь.Вы просто пытаетесь передать эти значения другим классам для выполнения вычислений на них?

Если вы на самом деле имеете в виду наследование, чтобы построить другой уже полученный ответ, вы можете изменить его следующим образом.

public class second extends main{
                     //inherit values of x and y
                     //manipulate values 
                     //Go to next class third
                    }

public class third extends second{
                     //inherit values of x and y from second class
                     //manipulate values 
                   }
0 голосов
/ 02 февраля 2012
public class Main {
                     int x=25;
                     int y =25;
                   //Go to next class second
                   aMethod(){
                    Second s = new Second();
                    s.manipulateValues(x,y);
                    }
                  }

public class Second {
                     //inherit values of x and y
                     //manipulate values 
                     //Go to next class third
                       public void manipulateValues(int x, int y){
                           //manipulate here  
                           Third t = new Third ();
                           s.manipulateHereToo(x,y);
                       }
                    }

public class Third {
                     //inherit values of x and y from second class
                     //manipulate values 
                      public void manipulateHereToo(int x, int y){
                             //manipulate again
                       }
                   }

Таким образом, вы не обязаны использовать наследство.

0 голосов
/ 02 февраля 2012

Вы можете наследовать от другого класса, используя ключевое слово extends.

public class First {
    int x = 25;
    int y = 25;
}
//Class Second inherits (extends) from class First.
//This class will inherit from First, all its values (x and y).
public class Second extends First {
    public Second() {
        //Here we change the values of x and y in Second's constructor.
        x = 26;
        y = 26;
    }
}
0 голосов
/ 02 февраля 2012
public class second extends main{
                     //inherit values of x and y
                     //manipulate values 
                     //Go to next class third
                    }

public class third extends main{
                     //inherit values of x and y from second class
                     //manipulate values 
                   }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...