עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

מהי הדרך הטובה והבטוחה ביותר לקריאת overloaded method בג'אווה?

Featured Replies

פורסם

נגיד ואני בונה תוכנה שאמורה לקלוט מס' משתנים ולהחזיר את הממוצא,

ועפ"י כמות המשתנים שהיא קלטה היא קוראת לmethod המתאים...


import java.util.*;

public class avarage
{

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String str;
int num1=0, num2=0, num3=0, ctr =0;
System.out.println("Enter nums:");
str = scan.nextLine();

scan = new Scanner(str);
while (scan.hasNextInt())
{
if (ctr == 0)
num1 = scan.nextInt();
else if (ctr == 1)
num2 = scan.nextInt();
else if (ctr == 2)
num3 = scan.nextInt();
ctr++;
}

if (ctr == 2)
System.out.println(doAvarage(num1, num2));
else if (ctr == 3)
System.out.println(doAvarage(num1, num2, num3));
}

public static float doAvarage(int num1, int num2)
{
float result;

result = (float)(num1 + num2) / 2;
return result;
}
public static float doAvarage(int num1, int num2, int num3)
{
float result;

result = (float)(num1 + num2+ num3) /3;
return result;
}
}


יש דרך טובה יותר לקרוא לoverloaded method המתאים או שרק לספור כמה פרמטרים נכנסו ועם משפט IF להחליט?

פורסם
  • מחבר

עוד שאלה (אחרונה להיום :-P):

האם יש לי דרך לגרום מmethods של אובייקט מסויים להיות נעולות בתנאי מסויים?

נגיד יש CLASS עם כל מיני METHODS ויש לה גם משתנה פרטי BOOLEAN isLocked.

אני יכול לגרום לMETHOD לא להתבצע עם isLocked ==true, או שאני חייב לעשות שינויים בMETHOD עצמה,

(נגיד לעשות שכל שורות הקוד יתבצעו רק עם isLocked == false)..

תודות :yelclap:

פורסם

נראה לי שאין דרך יותר טובה מלבדוק את זה בתוך המתודה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.