Java 我要找的构造函数和访问修饰符的组合是什么?
我有一个用户定义的类分数,它包含几个构造函数和成员方法。我想创建一个单独的“独立函数”类,它使用两个分数实例作为参数来创建第三个分数,或者修改并返回所传递的其中一个实例Java 我要找的构造函数和访问修饰符的组合是什么?,java,class,constructor,utility,Java,Class,Constructor,Utility,我有一个用户定义的类分数,它包含几个构造函数和成员方法。我想创建一个单独的“独立函数”类,它使用两个分数实例作为参数来创建第三个分数,或者修改并返回所传递的其中一个实例 class MyFractionProject{ public static void main(String[] args){ class Fraction{ private int numerator; private int denominator; public F
class MyFractionProject{
public static void main(String[] args){
class Fraction{
private int numerator;
private int denominator;
public Fraction(){
numerator = 0;
denominator = 1;
}//default constructor
public Fraction (int num, int denom){
numerator = num;
denominator = denom;
}//sample constructor
//other constructors
//accessors and mutators (setters and getters) are here
public Fraction multiply(Fraction otherFraction){
Fraction result = new Fraction(//multiply the Fractions);
return result;
}//sample member method, calls a constructor and accessor
//a bunch of other member methods are here
}//end Fraction
//New standalone utility class goes here:
class FractionUtility{
//suite of ~5 functions which take two Fraction objects as arguments
//and return a Fraction object, either one of the arguments or a new
//instance
public static FractionUtility UtilityMultiply(Fraction fr1, Fraction fr2){
//lots of stuff
}//Does this HAVE to return a FractionUtility, or can it return a Fraction?
//Can I declare variables outside the static FractionUtility methods if they
//will be needed every time the method is called? Will they be accessible
//inside the static methods? Do I need to instead declare them inside every
//static method so that they're visible each time I call the method?
}//end FractionUtility
//bunch of other stuff in main(), successfully uses Fraction but not
//FractionUtility
}//end main()
}
实用程序类需要与分数体分开定义。我需要几个不同的分数实例,但不需要实例化FractionUtility。这使它看起来像是一个静态类,但当我这样做时,它会抛出错误——通常非静态分数变量不能从静态上下文访问
我知道在main()之外定义这两个类然后导入它们是有意义的,但我不知道如何做,或者如果我这样做,会应用什么规则 看来您只是想在同一个文件中声明几个不相关的类 这就是静态内部类的用途 例如,您可以执行以下操作:
public class Something {
static class MyClass {
private int data = 0;
public MyClass() {
}
public MyClass(int data) {
this.data = data;
}
public MyClass makeNew( MyClass otherinstance ) {
MyClass result = new MyClass( this.data + otherinstance.data );
return result;
}
}
static class MyUtilityClass {
}
public static void main( String[] args ) {
MyClass myClass = new MyClass();
MyClass copy = myClass.makeNew( new MyClass() );
MyUtilityClass utilityClass = new MyUtilityClass();
}
}
MyUtilityClass
中的方法应该是静态的。这只意味着您无法在没有实例的情况下访问MyUtilityClass
的实例成员。这并不意味着您不能访问其他类中的实例成员,前提是您有一个可以访问它们的实例。请提供一个具体的例子,说明您需要做的事情以及完成该任务所需的MyClass
的所有成员。如前所述,您并没有真正提出一个可以回答的问题。这是不是main()
方法应该属于类MyClass
?它必须属于某一类。如果您试图编译一个只包含您提供的内容的Java源文件,那么编译器肯定会发出错误。类MyClass
和UtilityClass
是否需要在main()
方法中声明?这是允许的,但不寻常。实际上,在实例方法中是允许的。在static
方法中,如果可以在那里声明这些类,则可能需要声明这些类static
。成员MyClass.data
是否需要private
,如图所示?@JohnBollinger主方法属于包含整个程序的类,称之为MyProgram。我将编辑以澄清这一点。在main()中声明MyClass和UtilityClass是我处理静态和非静态引用问题的方法:在包含Utility类之前,它都是在main()中运行的,没有任何问题。MyClass.data确实需要是私有的,我们必须证明对访问器和变异器的理解。如果我想在main之外声明这些类而不引起静态和非静态方法之间的冲突,我该怎么做呢?如果您不在Java中指定某个元素的作用域,默认情况下它将是包私有的。例如,这意味着同一个包中的任何其他类都可以看到MyUtilityClass。如果您想使它对任何其他包中的任何其他类可见,只需将其公开即可。如果您只希望它们在此文件中可见,请将它们设置为私有。方法也是如此。当然,类可见性优先于方法可见性,因为如果您甚至看不到非公共的类,那么即使它们是公共的,您也不能对其调用任何方法。请注意,JLS术语是“静态嵌套”类。“内部类”是一个非静态的嵌套类。当我以这种方式声明MyClass时,当我想用各种不同的值来实例化它时,比如说用两个MyClass实例,一个int data==10,另一个data==20,这难道不会导致问题吗?当我在那个场景中实例化MyUtilityClass时,它将返回MyUtilityClass,而不是MyClass,对吗?我将编辑我的原始帖子以提供更多信息,我原本担心发布太多关于作业的细节,但如果我不这样做,显然不会有任何效果。很快就会更新。你的疑问似乎很基本,如果你读一本Java初学者的书,这些问题都会迎刃而解。有这么多,我不能推荐一个,但请注意,你不能只是通过问问题来学习编程语言,所以。。。至少,一旦你掌握了两种语言,那么你就可能通过阅读一个快速参考页面或一篇博客文章,然后询问任何看起来很棘手的问题来了解其他人。但是在那之前,你应该做艰苦的工作,从零开始学习,然后才能编写有意义的程序。@Pete,我想你对嵌套类是静态的意味着什么感到困惑。这意味着静态嵌套类的实例不与包含类的任何特定实例相关联。静态嵌套类的成员不一定是静态的。这样的类可以有任意数量的实例,每个实例都有自己独立的实例数据,就像任何其他类一样。但是,不能在方法内声明静态嵌套类——此类类必须直接在其包含类的主体内声明。