日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 静态类的实现_关于java:在类中实现静态方法

發布時間:2023/12/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 静态类的实现_关于java:在类中实现静态方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過一本書,我正在經歷:

"設計一個類名MyInteger。該類包含:

...等等等等等等...

如果此對象中的值分別為偶數,奇數或素數,則方法isEven(),isOdd()和isPrime()分別返回true。

如果指定的值分別為偶數,奇數或素數,則靜態方法isEven(int),isOdd(int)和isPrime(int)分別返回true。

靜態方法isEven(MyInteger),isOdd(MyInteger),isPrime(MyInteger),如果指定的值分別為偶數,奇數或素數,則它們返回true。

這是到目前為止我得到的。頂部易于使用object.isEven()實現。

第二,我假設這只是在不實際設置值和更改對象的情況下顯示結果?所以我可以做object.isEven(2)嗎?

最后一個...讓我很不高興。我不知道。 = /請幫幫我。提前致謝。

澄清:

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)???

???

您的班級有一些靜態方法,問題是什么?

什么是object.isEven()? 另外,請發布您的代碼。

首先,如果您不了解實例方法和靜態方法之間的區別,請這樣說。 我假設您確實了解這種區別。 第二和第三組方法的參數形式不同。 第二組使用普通的int值,而第三組使用MyInteger類的實例(可能在其中包含一個數值)。

您使用例如MyInteger.isEven(27)調用類的靜態方法。

您的方法應返回指定的boolean值。 那就是他們應該做的所有事情。

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;

}

}

現在的主要課程:

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");

}

}

將其視為指針,然后您可能要看一下這個問題。

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;

}

}

您將對MyInteger對象執行操作,而不僅僅是直接處理int。

假設您的私有變量和構造函數如下所示(我們不完全知道,因為它沒有發布):

private int myInt;

public MyInteger(int thisInt) {

myInt = thisInt;

}

您將需要實現一個訪問器方法,該方法將在MyInteger類的實例內返回myInt的值,然后在靜態方法中使用此訪問器方法來執行操作。

因此,作為訪問器方法:

public int getInt()

{

return myInt;

}

然后,您的靜態方法將以與其他程序相同的方式引用此方法。請注意,即使在類中,也必須指定MyInteger對象的用法:

public static boolean isEven(MyInteger myInteger)

{

//Code here

}

就調用靜態方法而言,它看起來像這樣:

MyInteger myInteger = new MyInteger(50);

MyInteger.isEven(myInteger);

在這里,您引用的是MyInteger對象的實例(myInteger),而不是原始的int,但是由于isEven沒有直接連接到特定對象,因此您必須告訴代碼在哪里可以找到isEven()方法,即MyInteger類。

這似乎是令人困惑的

boolean odd2 = MyInteger.isOdd(new MyInteger(5)); ?// static call

您使用MyInteger的實例作為參數傳遞。將MyInteger作為參數傳遞的另一種方法是:

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);

}

}

是! 這幫助了很多! :)

對于第二個,該方法屬于該類。但不是創建的對象。

如果您的代碼是這樣的:

MyInteger myInteger = new MyInteger(100);

你可以這樣調用方法

MyInteger.isEven(50);

要么

MyInteger.isEven(50);

與對象中設置的100個無關。

應當指出,第二種形式純粹是編譯器混亂。 myInteger引用可以為null,它仍然可以工作,因為它基于引用的類型,而不是引用所針對的對象(如果有)的類型。

總結

以上是生活随笔為你收集整理的java 静态类的实现_关于java:在类中实现静态方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。