史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   如何改成抽象類別 (http://forum.slime.com.tw/thread202002.html)

leowang 2007-04-04 09:33 PM

如何改成抽象類別
 
PHP 語法:

interface Data
{   
   public 
void showData();   
}
interface 
Test
{
   public 
void showScore();
   public 
double calcu();
}
class 
CStu implements Data,Test
{
   protected 
String id;    // 學號
   
protected String name;  // 姓名 
   
protected int mid;      // 期中考成績
   
protected int finl;     // 期末考成績
   
protected int common;   // 平時成績
       
   
public CStu(String s1,String s2,int m,int f,int c)
   {
      
id=s1;
      
name=s2;
      
mid=m;
      
finl=f;   
      
common=c;   
   }
   public 
void show()
   {
      
showData();
      
showScore();
   }
   public 
void showScore()
   {
      
System.out.println("期中考成績:"+mid);
      
System.out.println("期末考成績:"+finl);
      
System.out.println("平時成績:"+common);
      
System.out.println("學期成績:"+calcu());
   }
   public 
void showData()
   {
      
System.out.println("學號:"+id);
      
System.out.println("姓名:"+name);
   }
   public 
double calcu()
   {
      return (
mid*0.3+finl*0.3+common*0.4);      
   }
}

public class 
hw11_11
{
   public static 
void main(String args[])
   {
      
CStu stu=new CStu("940001","Fiona",90,92,85);
      
stu.show();
   }


請問上列程式碼如何改成抽象類別的形式

snoopy 2007-04-05 02:23 AM

雖然我不知道為什麼你要這要做
不過很好改
語法:

interface Data
{   
  public void showData();   
}
interface Test
{
  public void showScore();
  public double calcu();
}
abstract class CStu implements Data,Test
{
  protected String id;    // 學號
  protected String name;  // 姓名 
  protected int mid;      // 期中考成績
  protected int finl;    // 期末考成績
  protected int common;  // 平時成績
       
  public CStu(String s1,String s2,int m,int f,int c)
  {
      id=s1;
      name=s2;
      mid=m;
      finl=f;   
      common=c;   
  }
  public void show()
  {
      showData();
      showScore();
  }
  public void showScore()
  {
      System.out.println("期中考成績:"+mid);
      System.out.println("期末考成績:"+finl);
      System.out.println("平時成績:"+common);
      System.out.println("學期成績:"+calcu());
  }
  public void showData()
  {
      System.out.println("學號:"+id);
      System.out.println("姓名:"+name);
  }
  public double calcu()
  {
      return (mid*0.3+finl*0.3+common*0.4);     
  }
}

class SubClass extends CStu{
        public SubClass(String s1,String s2,int m,int f,int c){
                super(s1,s2,m,f,c);
        }
}

public class App
{
  public static void main(String args[])
  {
      CStu stu=new SubClass("940001","Fiona",90,92,85);
      stu.show();
  }
}


leowang 2007-04-05 10:49 AM

沒有辦法把介面改成抽象類別嗎........

snoopy 2007-04-05 04:45 PM

可以啊
不過 java 是單一繼承制
所以你只能繼承其中一個類別

你想把兩個介面都改抽象類別
然後繼承這兩個類別就不行

leowang 2007-04-05 05:52 PM

引用:

作者: snoopy (文章 1693035)
可以啊
不過 java 是單一繼承制
所以你只能繼承其中一個類別

你想把兩個介面都改抽象類別
然後繼承這兩個類別就不行

那依大大所說 要如何來做修改呢

snoopy 2007-04-05 06:07 PM

語法:

interface Data {
        public void showData();
}

abstract class Test {
        abstract public void showScore();

        abstract public double calcu();
}

class CStu extends Test implements Data {
        protected String id; // 學號

        protected String name; // 姓名 

        protected int mid; // 期中考成績

        protected int finl; // 期末考成績

        protected int common; // 平時成績

        public CStu(String s1, String s2, int m, int f, int c) {
                id = s1;
                name = s2;
                mid = m;
                finl = f;
                common = c;
        }

        public void show() {
                showData();
                showScore();
        }

        public void showScore() {
                System.out.println("期中考成績:" + mid);
                System.out.println("期末考成績:" + finl);
                System.out.println("平時成績:" + common);
                System.out.println("學期成績:" + calcu());
        }

        public void showData() {
                System.out.println("學號:" + id);
                System.out.println("姓名:" + name);
        }

        public double calcu() {
                return (mid * 0.3 + finl * 0.3 + common * 0.4);
        }
}

public class App {
        public static void main(String args[]) {
                CStu stu = new CStu("940001", "Fiona", 90, 92, 85);
                stu.show();
        }
}


leowang 2007-04-06 11:22 AM

引用:

作者: snoopy (文章 1693081)
語法:

interface Data {
        public void showData();
}

abstract class Test {
        abstract public void showScore();

        abstract public double calcu();
}

class CStu extends Test implements Data {
        protected String id; // 學號

        protected String name; // 姓名 

        protected int mid; // 期中考成績

        protected int finl; // 期末考成績

        protected int common; // 平時成績

        public CStu(String s1, String s2, int m, int f, int c) {
                id = s1;
                name = s2;
                mid = m;
                finl = f;
                common = c;
        }

        public void show() {
                showData();
                showScore();
        }

        public void showScore() {
                System.out.println("期中考成績:" + mid);
                System.out.println("期末考成績:" + finl);
                System.out.println("平時成績:" + common);
                System.out.println("學期成績:" + calcu());
        }

        public void showData() {
                System.out.println("學號:" + id);
                System.out.println("姓名:" + name);
        }

        public double calcu() {
                return (mid * 0.3 + finl * 0.3 + common * 0.4);
        }
}

public class App {
        public static void main(String args[]) {
                CStu stu = new CStu("940001", "Fiona", 90, 92, 85);
                stu.show();
        }
}


非常感謝大大的解答......大概知道怎修改了


所有時間均為台北時間。現在的時間是 09:20 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1