java 静态类的实现_关于java:在类中实现静态方法
通過一本書,我正在經(jīng)歷:
"設(shè)計(jì)一個(gè)類名MyInteger。該類包含:
...等等等等等等...
如果此對(duì)象中的值分別為偶數(shù),奇數(shù)或素?cái)?shù),則方法isEven(),isOdd()和isPrime()分別返回true。
如果指定的值分別為偶數(shù),奇數(shù)或素?cái)?shù),則靜態(tài)方法isEven(int),isOdd(int)和isPrime(int)分別返回true。
靜態(tài)方法isEven(MyInteger),isOdd(MyInteger),isPrime(MyInteger),如果指定的值分別為偶數(shù),奇數(shù)或素?cái)?shù),則它們返回true。
這是到目前為止我得到的。頂部易于使用object.isEven()實(shí)現(xiàn)。
第二,我假設(shè)這只是在不實(shí)際設(shè)置值和更改對(duì)象的情況下顯示結(jié)果?所以我可以做object.isEven(2)嗎?
最后一個(gè)...讓我很不高興。我不知道。 = /請(qǐng)幫幫我。提前致謝。
澄清:
1。
public boolean isEven(){
// code
}
MyInteger object = new MyIntger(50);
object.isEven();
2。
public boolean isEven(int num){
// code
}
MyInteger.isEven(50)???
3。
public boolean isEven(int MyInteger)???
???
您的班級(jí)有一些靜態(tài)方法,問題是什么?
什么是object.isEven()? 另外,請(qǐng)發(fā)布您的代碼。
首先,如果您不了解實(shí)例方法和靜態(tài)方法之間的區(qū)別,請(qǐng)這樣說。 我假設(shè)您確實(shí)了解這種區(qū)別。 第二和第三組方法的參數(shù)形式不同。 第二組使用普通的int值,而第三組使用MyInteger類的實(shí)例(可能在其中包含一個(gè)數(shù)值)。
您使用例如MyInteger.isEven(27)調(diào)用類的靜態(tài)方法。
您的方法應(yīng)返回指定的boolean值。 那就是他們應(yīng)該做的所有事情。
class MyInteger {
int number;
// CONSTRUCTOR
public MyInteger(int a) {
number = a;
}
public int getNumber() {
return number;
}
static boolean isEven(MyInteger myint) {
if (myint.getNumber() % 2 == 0)
return true;
else
return false;
}
}
現(xiàn)在的主要課程:
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyInteger myInteger=new MyInteger(10);
boolean result=MyInteger.isEven(myInteger);
if(result==true)
System.out.println("true result");
else
System.out.println("false result");
}
}
將其視為指針,然后您可能要看一下這個(gè)問題。
public class MyInteger {
private int value;
public MyInteger(int value) {
super();
this.value = value;
}
public static boolean isPrime(int value) {
// I would increment counter then test if the result of value modulo counter
// (that is if value % counter != 0) until counter >= square_root(value).
// Then the value is prime, otherwise
return false;
}
public static boolean isEven(int value) {
return (value & 1) == 0;
}
public static boolean isEven(MyInteger m) {
return isEven(m.value);
}
public static boolean isPrime(MyInteger m) {
return isPrime(m.value);
}
public static boolean isOdd(int value) {
return !isEven(value);
}
public static boolean isOdd(MyInteger m) {
return isOdd(m.value);
}
public boolean isEven() {
return isEven(this.value);
}
public boolean isOdd() {
return isOdd(this.value);
}
public boolean isPrime() {
return isPrime(value);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
您將對(duì)MyInteger對(duì)象執(zhí)行操作,而不僅僅是直接處理int。
假設(shè)您的私有變量和構(gòu)造函數(shù)如下所示(我們不完全知道,因?yàn)樗鼪]有發(fā)布):
private int myInt;
public MyInteger(int thisInt) {
myInt = thisInt;
}
您將需要實(shí)現(xiàn)一個(gè)訪問器方法,該方法將在MyInteger類的實(shí)例內(nèi)返回myInt的值,然后在靜態(tài)方法中使用此訪問器方法來執(zhí)行操作。
因此,作為訪問器方法:
public int getInt()
{
return myInt;
}
然后,您的靜態(tài)方法將以與其他程序相同的方式引用此方法。請(qǐng)注意,即使在類中,也必須指定MyInteger對(duì)象的用法:
public static boolean isEven(MyInteger myInteger)
{
//Code here
}
就調(diào)用靜態(tài)方法而言,它看起來像這樣:
MyInteger myInteger = new MyInteger(50);
MyInteger.isEven(myInteger);
在這里,您引用的是MyInteger對(duì)象的實(shí)例(myInteger),而不是原始的int,但是由于isEven沒有直接連接到特定對(duì)象,因此您必須告訴代碼在哪里可以找到isEven()方法,即MyInteger類。
這似乎是令人困惑的
boolean odd2 = MyInteger.isOdd(new MyInteger(5)); ?// static call
您使用MyInteger的實(shí)例作為參數(shù)傳遞。將MyInteger作為參數(shù)傳遞的另一種方法是:
MyInteger num = new MyInteger(5);
boolean odd2 = MyInteger.isOdd(num); ?// static call
class MyInteger{
int num;
public MyIntger(int num){
this.num = num;
}
// Method 1
public static boolean isOdd(int num){
...
}
// Method 2
public boolean isOdd(){
...
}
// Method 3
public static boolean isOdd(MyInteger num){
...
}
}
public class TestMyInteger{
public static void main(String[] args){
// Method 1 call
boolean odd1 = MyIntger.isOdd(5); ? ?// static call
// Method 3 call
boolean odd2 = MyInteger.isOdd(new MyInteger(5)); ?// static call
// Method 2 call
MyIntger num = new MyIntger(5); ?// create instance
boolean odd3 = num.isOdd(); ? // instance call
System.out.println(odd1);
System.out.println(odd2);
System.out.println(odd3);
}
}
是! 這幫助了很多! :)
對(duì)于第二個(gè),該方法屬于該類。但不是創(chuàng)建的對(duì)象。
如果您的代碼是這樣的:
MyInteger myInteger = new MyInteger(100);
你可以這樣調(diào)用方法
MyInteger.isEven(50);
要么
MyInteger.isEven(50);
與對(duì)象中設(shè)置的100個(gè)無關(guān)。
應(yīng)當(dāng)指出,第二種形式純粹是編譯器混亂。 myInteger引用可以為null,它仍然可以工作,因?yàn)樗谝玫念愋?#xff0c;而不是引用所針對(duì)的對(duì)象(如果有)的類型。
總結(jié)
以上是生活随笔為你收集整理的java 静态类的实现_关于java:在类中实现静态方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 249元 一加Buds Ace降噪耳机开
- 下一篇: java第九章实验报告_2019JAVA