java 使用私有静态布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12648789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Private Static Boolean
提问by user1687366
I am a brand new student of java and I have been searching google and my textbook to figure out what exactly it means to use a private static boolean, though none of it is really making any sense so I figured I'd ask the experts. My problem is that I need to use the private static class, though I don't know how to draw the data I need from main (which was kindly already written by my professor). Do you have any suggestions or insight? I'm basically converting code that I have already written in the form of nested loops into a private class.
我是一个全新的 java 学生,我一直在搜索谷歌和我的教科书,以弄清楚使用私有静态布尔值究竟意味着什么,尽管它没有任何意义,所以我想我会问专家。我的问题是我需要使用私有静态类,尽管我不知道如何从 main 中提取我需要的数据(我的教授已经写好了)。你有什么建议或见解吗?我基本上是将已经以嵌套循环形式编写的代码转换为私有类。
edits: this is what I have to work with (I'm not looking for a handout)
编辑:这是我必须使用的(我不是在寻找讲义)
import java.io.*;
import java.util.*;
public class Foo
{
public static void main (String[] args) throws Exception
{
int lo = 0;
int hi = 0;
String infileName;
if (args.length < 3)
{
System.out.println("CMD LINE INPUT ERROR: Must enter 2 numbers followed by name of input file on cmd line.");
System.exit(0);
}
lo = Integer.parseInt(args[0]);
hi = Integer.parseInt(args[1]);
infileName = args[2];
for( int n = lo; n<=hi; n++)
if (isPrime(n))
System.out.print(n + " ");
System.out.println();
for( int n=lo; n<=hi; n++)
if (isPerfect(n))
System.out.print(n + " ");
System.out.println();
while (infile.ready())
{
String line = infile.readLine();
if ( isPalindrome( line ) )
System.out.println( line + " IS a palindrome");
else
System.out.println( line + "is NOT a palindrome");
}
infile.close();
}
}
the assignment is in three parts: finding prime numbers, perfect numbers, and palindromes. for an example of part 1, I have previously used the following to find prime numbers
作业分为三部分:求素数、完全数和回文。对于第 1 部分的示例,我以前使用以下内容来查找素数
for(int i= lo; i < hi; i++)
{
boolean isPrime = true;
for(int j=2; j < i ; j++)
{
if(i % j == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
System.out.print(i + " ");
}
System.out.println();
I'm mostly not sure how to apply what I already know in a new way.
我大多不确定如何以新的方式应用我已经知道的东西。
回答by PermGenError
Firstly, you mentioned private static class, you can have a static inner class. but you cant mark a class(outer class) with private modifieri.e., you cant have a private class(outer class). outer Classes can only be marked with publicor finalor abstractor default(no modifier)
首先,你提到了私有静态类,你可以有一个静态内部类。但是你不能用私有修饰符标记一个类(外部类),即你不能有一个私有类(外部类)。外部类只能用public或final或abstract或default (无修饰符)标记
private static boolean:
私有静态布尔值:
privateis an access modifierwhich can be used for instance variables and methods. private variables and private methods are only confined that the class which they were declared in.
private是一个访问修饰符,可用于实例变量和方法。私有变量和私有方法只限于声明它们的类。
staticis an non access modifierwhich can be used for variables and methods.static variables are shared by all instances of a class.
static是一个非访问修饰符,可用于变量和方法。静态变量由类的所有实例共享。
booleanis a primitive datatypewhich can be true or false.
boolean是一种原始数据类型,可以是 true 也可以是 false。
if you declare a boolean variable as private static like
如果您将布尔变量声明为私有静态,例如
class MyClass {
private static boolean bool;
}
1. this bool variable can only accessed from with in the class(MyClass) which it is defined.
1. 这个 bool 变量只能从它定义的类(MyClass)中访问。
2.All the instances created for this class(MyClass) share this bool variale.
2.为这个类(MyClass)创建的所有实例共享这个bool变量。
回答by BillToWin
Your use of the word "class" is confusing, although I believe you actually mean "method" rather than "class"
您对“类”一词的使用令人困惑,尽管我相信您实际上是指“方法”而不是“类”
By "private static boolean" I'm guessing you're talking about the headerof a methodwhich is a block of code and can be called (invoked) at any point in a program. A method often takes in a certain amount of parameters and often, but not always, returns a value.
通过“私有静态布尔值”,我猜你在谈论一个方法的头,它是一个代码块,可以在程序的任何点被调用(调用)。一个方法通常接受一定数量的参数,并且经常但不总是返回一个值。
private: restricts the method's use to the classit is defined in.
private:将方法的使用限制在定义它的类中。
static: a method which is static can be accessed directly by the class name and doesn't need any object, i.e. ClassName.methodName() vs. ObjectName.methodName().
static:静态方法可以通过类名直接访问,不需要任何对象,即ClassName.methodName() vs. ObjectName.methodName()。
boolean: simply means that a booleanvalue is returned by the method.
boolean: 简单的意思是方法返回一个布尔值。
your code might look something like this:
您的代码可能如下所示:
private static boolean isBiggerThanZero( int number)
{
boolean isBigger = false;
//Do something with number or test it for a condition
if( number > 0 )
isBigger = true;
return isBigger;
}
回答by awiebe
You do not take data from main, main jumps to other functions and passes the required data to them. If you're talking about member variables, basically just move what is currently local, but used more than once to just under the class block and not in any function.
您不从 main 中获取数据,main 跳转到其他函数并将所需的数据传递给它们。如果你在谈论成员变量,基本上只是移动当前本地的内容,但不止一次地在类块下而不是在任何函数中使用。