遍匈 / Java縮殻 /

Java 渇嵆

<貧匯准 和匯准>
渇嵆頁峺壓中?斤嵆園殻?OOP?嶄幹秀渇嵆窃議嬬薦。
渇嵆窃茅阻音嬬糞箭晒斤嵆岻翌?窃議凪万孔嬬卆隼贋壓?撹埀延楚、撹埀圭隈才更夛圭隈議恵諒圭塀才噸宥窃匯劔。
喇噐渇嵆窃音嬬糞箭晒斤嵆?侭參渇嵆窃駅倬瓜写覚?嘉嬬瓜聞喘。匆頁咀葎宸倖圻咀?宥械壓譜柴竣粁畳協勣音勣譜柴渇嵆窃。
幻窃淫根阻徨窃鹿栽議械需議圭隈?徽頁喇噐幻窃云附頁渇嵆議?侭參音嬬聞喘宸乂圭隈。

渇嵆窃?

聞喘購囚忖abstract栖蕗苧渇嵆窃。乎購囚忖壓窃議蕗苧嶄慧壓class購囚忖議念中。
/* File name : Employee.java */
public abstract class Employee
{
 private String name;
 private String address;
 private int number;
 public Employee(String name, String address, int number)
 {
 System.out.println("Constructing an Employee");
 this.name = name;
 this.address = address;
 this.number = number;
 }
 public double computePay()
 {
 System.out.println("Inside Employee computePay");
 return 0.0;
 }
 public void mailCheck()
 {
 System.out.println("Mailing a check to " + this.name
 + " " + this.address);
 }
 public String toString()
 {
 return name + " " + address + " " + number;
 }
 public String getName()
 {
 return name;
 }
 public String getAddress()
 {
 return address;
 }
 public void setAddress(String newAddress)
 {
 address = newAddress;
 }
 public int getNumber()
 {
 return number;
 }
}
廣吭欺乎Employee窃短嗤焚担音揖?勝砿乎窃頁渇嵆窃?徽頁万挽隼嗤3倖撹埀延楚?7倖撹埀圭隈才1倖更夛圭隈。
孖壓泌惚低晦編泌和議箭徨?
/* File name : AbstractDemo.java */
public class AbstractDemo
{
 public static void main(String [] args)
 {
 /* Following is not allowed and would raise error */
 Employee e = new Employee("George W.", "Houston, TX", 43);
 
 System.out.println("
 Call mailCheck using 
 Employee reference--");
 e.mailCheck();
 }
}
輝低晦編園咎AbstractDemo窃扮?氏恢伏泌和危列?
Employee.java:46: Employee is abstract; cannot be instantiated
 Employee e = new Employee("George W.", "Houston, TX", 43);
 ^
1 error

写覚渇嵆窃?

厘断嬬宥狛匯違議圭隈写覚Employee窃?
/* File name : Salary.java */
public class Salary extends Employee
{
 private double salary; //Annual salary
 public Salary(String name, String address, int number, double
 salary)
 {
 super(name, address, number);
 setSalary(salary);
 }
 public void mailCheck()
 {
 System.out.println("Within mailCheck of Salary class ");
 System.out.println("Mailing check to " + getName()
 + " with salary " + salary);
 }
 public double getSalary()
 {
 return salary;
 }
 public void setSalary(double newSalary)
 {
 if(newSalary >= 0.0)
 {
 salary = newSalary;
 }
 }
 public double computePay()
 {
 System.out.println("Computing salary pay for " + getName());
 return salary/52;
 }
}
勝砿厘断音嬬糞箭晒匯倖Employee窃議斤嵆?徽頁泌惚厘断糞箭晒匯倖Salary窃斤嵆?乎斤嵆繍貫Employee窃写覚3倖撹埀延楚才7倖撹埀圭隈。
/* File name : AbstractDemo.java */
public class AbstractDemo
{
 public static void main(String [] args)
 {
 Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP",
 3, 3600.00);
 Salary e = new Salary("John Adams", "Boston, MA",
 2, 2400.00);
 
 System.out.println("Call mailCheck using
 Salary reference --");
 s.mailCheck();
 System.out.println("
 Call mailCheck using
 Employee reference--");
 e.mailCheck();
 }
}
塰佩潤惚泌和?
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
 
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.

渇嵆圭隈?

泌惚低?譜柴宸劔匯倖窃?乎窃淫根匯倖蒙艶議撹埀圭隈?乎圭隈議醤悶糞孖喇万議徨窃鳩協?椎担低辛參壓幻窃嶄蕗苧乎圭隈葎渇嵆圭隈。
Abstract購囚忖揖劔辛參喘栖蕗苧渇嵆圭隈?渇嵆圭隈峪淫根匯倖圭隈兆?遇短嗤圭隈悶。
渇嵆圭隈短嗤協吶?圭隈兆朔中岷俊効匯倖蛍催?遇音頁雑凄催。
public abstract class Employee
{
 private String name;
 private String address;
 private int number;
 
 public abstract double computePay();
 
 //Remainder of class definition
}
蕗苧渇嵆圭隈氏夛撹參和曾倖潤惚?
  • 泌惚匯倖窃淫根渇嵆圭隈?椎担乎窃駅倬頁渇嵆窃。
  • 販採徨窃駅倬嶷亟幻窃議渇嵆圭隈?賜宀蕗苧徭附葎渇嵆窃。
写覚渇嵆圭隈議徨窃駅倬嶷墮乎圭隈。倦夸?乎徨窃匆駅倬蕗苧葎渇嵆窃。恷嶮?駅倬嗤徨窃糞孖乎渇嵆圭隈?倦夸?貫恷兜議幻窃欺恷嶮議徨窃脅音嬬喘栖糞箭晒斤嵆。
泌惚Salary窃写覚阻Employee窃?椎担万駅倬糞孖computePay()圭隈?
/* File name : Salary.java */
public class Salary extends Employee
{
 private double salary; // Annual salary
 
 public double computePay()
 {
 System.out.println("Computing salary pay for " + getName());
 return salary/52;
 }
 
 //Remainder of class definition
}
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /