Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 郭翔宇:使用Java物件導向概念實作能接收 1 2 3 號球的box1,只能接收 2 3 號球的box2,只能接收 3 號球的box3。 */
- /*
- 黃珍珍10
- 黃珍珍Taiwan 程式語言讀書會 [JAVA,C,C++,C#,VB...等不拘]
- 35分鐘 ·
- 想請問一個邏輯問題轉成程式 (程式我是用java寫)
- "這真的不是作業" 是我寫的程式裡有一小段這種邏輯 但我把它轉成箱子跟球的問題
- 題目:
- 有編號123的箱子
- 1號可以放編號123的球
- 2號可以放編號23的球
- 3號只能放編號3
- 想請問若今天有很多顆 可以重複的球
- 要如何擺才能讓球的數字加起來最大呢?
- https://www.facebook.com/photo.php?fbid=603186333433235&set=gm.2232585740289016&type=3&theater&ifg=1
- */
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Kuo, Hsiang-Yu
- */
- class Three {
- int id = 3;
- }
- class Two extends Three {
- int id = 2;
- }
- class One extends Two {
- int id = 1;
- }
- public class MainClass {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- int sum = 0;
- One one = new One();
- Two two = new Two();
- Three three = new Three();
- System.out.println("box1(one) = " + box1(one));
- System.out.println("box1(two) = " + box1(two));
- System.out.println("box1(three) = " + box1(three));
- System.out.println("box2(one) = " + box2(one));
- System.out.println("box2(two) = " + box2(two));
- System.out.println("box2(three) = " + box2(three));
- System.out.println("box3(one) = " + box3(one));
- System.out.println("box3(two) = " + box3(two));
- System.out.println("box3(three) = " + box3(three));
- }
- private static int box1(Three three) {
- //throw new UnsupportedOperationException("Not supported yet.");
- //To change body of generated methods, choose Tools | Templates.
- /* 編號1的箱子,可以放編號 1 2 3 的球 */
- if (three instanceof One) {
- One one = (One) three;
- return one.id;
- } else if (three instanceof Two) {
- Two two = (Two) three;
- return two.id;
- } else {
- return three.id;
- }
- }
- private static int box2(Three three) {
- //throw new UnsupportedOperationException("Not supported yet.");
- //To change body of generated methods, choose Tools | Templates.
- /* 編號2的箱子,可以放編號 2 3 的球 */
- if (three instanceof One) {
- /* 編號2的箱子拒絕接受 1號球,所以回傳0 */
- System.out.println("box2 拒絕 1號球。");
- return 0;
- } else if (three instanceof Two) {
- Two two = (Two) three;
- return two.id;
- } else {
- return three.id;
- }
- }
- private static int box3(Three three) {
- //throw new UnsupportedOperationException("Not supported yet.");
- //To change body of generated methods, choose Tools | Templates.
- /* 編號3的箱子,可以放編號 3 的球 */
- if (three instanceof One) {
- /* 編號3的箱子拒絕接受 1號球,所以回傳0 */
- System.out.println("box3 拒絕 1號球。");
- return 0;
- } else if (three instanceof Two) {
- /* 編號3的箱子拒絕接受 2號球,所以回傳0 */
- System.out.println("box3 拒絕 2號球。");
- return 0;
- } else {
- return three.id;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement