如何通过1个类访问另一个类中的私有或公共变量 [英] How do I access private or public variables in another class through 1 class

查看:371
本文介绍了如何通过1个类访问另一个类中的私有或公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才第一次问了一个问题

请求有关正确编码设计的帮助 [ ^ ]



也许我只是个傻瓜,但我无法赶上他们在那里提到的所有,尽管我做了一些研究并得出结论,我认为静态变量是我们在所有对象共有时分配给变量的东西。但是我仍然坚持我应该如何从另一个类中访问一个类中的变量。(如果我对静态变量的想法是错误的,请纠正我)这是我想出的一个代码,使问题更容易了解



Hellow all i first asked a question here
Requesting help regarding proper coding designs[^]

maybe i am just a fool but i could not catch up with what they had mentioned there all though i did some research and came into a conclusion where i think that a static variable is something that we assign to a variable when that is common to all the objects . However i am still stuck with how i should access variables that are in one class from another .(also pls correct me if my idea on static variables is wrong)This is a code that i just came up with to make the question more easier to understand

package testing1;

import java.util.Scanner;

class HerDetails {

    public String her_Name = null;
    public int her_age = 0;
    public String her_eye_colour = null;
    private String her_Hair_Colour = null;


    public void getHerDetails() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Her name");
        her_Name = sc.next();
        System.out.println("Enter her age");
        her_age = sc.nextInt();
        System.out.println("Enter her eye colour");
        her_eye_colour = sc.next();
        System.out.println("Enter her hair colour");
        her_Hair_Colour = sc.next();


    }

    public void herDetails() {

        System.out.print(her_Name);
        System.out.println(her_age);
        System.out.println(her_eye_colour);


    }
}

class  HisDetails{

        public String his_Name=null;
        private int his_Age;
        private String his_eye_colour;



        public void getHisDetails(){
        Scanner sc1=new Scanner(System.in);
            System.out.println("Enter His name");
            his_Name= sc1.next();
            System.out.println("Enter His Age");
            his_Age=sc1.nextInt();
            System.out.println("Enter his eye colour");
            his_eye_colour= sc1.next();



        }
        public void displayHisDetails(){
            System.out.println(his_Name);
            System.out.println(his_Age);
            System.out.println(his_eye_colour);

        }



}




public class Main {

    public static void main(String[] args) {

         HerDetails h1=new HerDetails();
         HisDetails h2=new HisDetails();
        h1.getHerDetails();
        h2.getHisDetails();
        h1.herDetails();
         h2.displayHisDetails();

    }
}





所以我怎么做这个想象我想创造一个名为itsdetails的类,我将插入数据,如



so how do i go about doing this imagine i wanted to create a class called "theirdetails" where i am going to insert data like

int theDayTheyMet

int ageDifference

或String

theirNamesTogether

我认为这对你有意义。因此,为了解决这个问题,我显然需要使用两个人的年龄来找到与其他两个班级及其名字的区别,以便将他们的名字放在一起。如何将从第一个类输入的值传递给另一个类?我想知道正确的方法吗?



我的尝试:



我不确定iv读了很多但没有运气我知道安装者和吸气剂工作,但是我应该使用的仍然是一个疑问?意思是我还不知道我的基础知识。请赐教

which i think would make sense to you hopefully . So inorder to go about this i clearly need to use the ages of the two people to find the difference from the other two classes and their names to get their names together . How do i get those values entered from the first class to another class ? i want to know the correct way?

What I have tried:

Im not sure iv read alot but not had any luck i know that setters and getters work , but is that what i should use remains a doubt ? meaning i still dont know my basics.? please enlighten me

推荐答案

Java是 OOP [ ^ ]语言,因此您可以使用继承,多态性 [ ^ ]等

以上链接的示例:

Java is OOP[^] language and therefore you can use inheritance, polymorphism[^], etc.
An example from above link:
class Teacher {
   String designation = "Teacher";
   String collegeName = "Beginnersbook";
   void does(){
	System.out.println("Teaching");
   }
}

public class PhysicsTeacher extends Teacher{
   String mainSubject = "Physics";
   public static void main(String args[]){
	PhysicsTeacher obj = new PhysicsTeacher();
	System.out.println(obj.collegeName);
	System.out.println(obj.designation);
	System.out.println(obj.mainSubject);
	obj.does();
   }
}





以同样的方式,你可以扩展 Person 类通过创建 Man Woman 派生自 Person的类



关于此声明:



On the same manner, you can extend Person class by creating Man and Woman classes which derived from Person.

As to this statement:

引用:

我想创建一个名为itsdetails的类,我要插入数据,例如

int theDayTheyMet



int ageDifference

或String

theirNamesTogether

i wanted to create a class called "theirdetails" where i am going to insert data like
int theDayTheyMet
and
int ageDifference
or String
theirNamesTogether





你总是可以创建情侣配对类可以容纳2个公共成员:男人女人





You can always create a Couple or Pair class which may hold 2 public members: Man and Woman.

class Couple {
    private Man his = new Man(...);
    private Woman her = new Woman(...);

    //another members, methods here
    
}



因为 Man 女人基类是,您可以将 Couple 类简化为:


Because Man and Woman base class is Person, you can simplify your Couple class to:

class Couple {
    private List<Person> persons = new ArrayList<Person>();

    //another members, methods here
}



列表 [ ^ ]和 ArrayList [ ^ ]提供了几种添加/迭代/删除元素的方法。





我存储日期 [ ^ ]。您可以这样计算年龄: year_of_current_date - year_of_birth

请参阅:

计算Java出生日期的年龄 - Memorynotfound [ ^ ]

date - 如何用Java计算某人的年龄? [ ^ ]


A List[^] and an ArrayList[^] provide several methods to add/iterate/remove elements.


I'd store date[^] of birth instead of age. You can calculate age this way: year_of_current_date - year_of_birth.
See:
Calculate Age from date of birth in Java - Memorynotfound[^]
date - How do I calculate someone's age in Java?[^]


这篇关于如何通过1个类访问另一个类中的私有或公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆