佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: jasonmun

Effective Java - General Programming

  [复制链接]
 楼主| 发表于 6-2-2011 12:01 AM | 显示全部楼层
伯乐希望马儿好(code quality),也希望马儿不吃草(code得快),大大有什么对策呢?
User. 发表于 5-2-2011 10:45 PM


好的马儿也会[选]自己的伯乐的..
回复

使用道具 举报


ADVERTISEMENT

发表于 6-2-2011 12:22 AM | 显示全部楼层
好的马儿也会[选]自己的伯乐的..
jasonmun 发表于 6-2-2011 12:01 AM


对于programmer这条路有些什么忠告吗?
回复

使用道具 举报

发表于 16-12-2011 02:55 PM | 显示全部楼层
请问下,有一些疑问, toString的function是什么?读了很多forum都不知道。。
回复

使用道具 举报

发表于 17-12-2011 01:27 AM | 显示全部楼层
回复 23# rhfh


    toString 是用String来描述object的一个method。例如下面的dog class, 里面override了object的toString method。create出来的dog object 就可以用toString method来描述dog object。

  1. private class Dog{
  2.                 @Override
  3.                 public String toString()
  4.                 {
  5.                         return "A 4 legged animal. Man's best friend.";
  6.                 }
  7.         }
复制代码
  1. System.out.println(new Dog().toString());
复制代码
回复

使用道具 举报

发表于 17-12-2011 01:31 AM | 显示全部楼层
顺便介绍这一本Head First Design Pattern。个人觉得也非常棒。
http://shop.oreilly.com/product/9780596007126.do
回复

使用道具 举报

发表于 17-12-2011 10:28 AM | 显示全部楼层
回复 24# algorithm


   解释到很仔细。。谢谢你。。  请问toString的format都是一样的吗?
  public string toString ( );
  {
  return (abcde);
  }

我可以这样做吗?

System.out.println (" A dog is a 4 legged animal. Man's best friend");   
without going through toString?

真的很需要你的帮助,因为要做一个cinema booking system..两个星期后就要交了...
回复

使用道具 举报

Follow Us
发表于 17-12-2011 10:19 PM | 显示全部楼层
回复 26# rhfh
toString 是 Object Class 里的method。Object Class 是class hierarchy里的Root Class,所有Class最终的superclass就是Object Class。
如果你要用toString你就必须override。Override是不可以更改format的,你只可以改变里面的程序。

  public string toString ( ); <<< String “S” 大写,semicolon “;” 也不应该有
  {
  return (abcde); <<< toString的return type必须是String,如果abcde是String就可以,但是括弧就不必了。
  }

System.out.println是用system的outputstream来print(显示)东西。你之前问toString的function,然后我用dog class来解释。
我只是用
System.out.println来显示dog object的toString。
你以下的code只是pass in String " A dog is a..."给println method,跟toString根本没有关系。
System.out.println (" A dog is a 4 legged animal. Man's best friend");

真的很需要你的帮助,因为要做一个cinema booking system..两个星期后就要交了...<<< 祝你好运
回复

使用道具 举报

发表于 13-1-2012 11:37 AM | 显示全部楼层
34. Refer to objects by their interfaces

这个habit可以帮助开发者把specifications (interfaces)和implementations (classes)分开,提高代码的扩展性和稳定性。例如,开发团队经理可以控制specification的更变,所有开发者都必须遵循interfaces来编写implementation的代码,这样不但可以增加产品的稳定性,而且代码管理起来也方便。
举个简单的例子,以下是一个系统用户登入的代码,这个系统同时可以接受一种以上的登入方式,用inteface的话就非常简洁,而且扩展性高。
  1. /**
  2. * Authenticator Interface
  3. * @author Kugua
  4. */
  5. public interface Authenticator {
  6.         /**
  7.          *        Authenticate user based on user's environment settings
  8.          *  @param username User's username
  9.          *  @param password User's plain-text password
  10.          *  @return Identity object of the authenticated user. null object will be returned if authentication failed.
  11.          */
  12.         public Identity authenticate(String username, String password);
  13. }
复制代码




接下来是三种implementations
  1. public class DatabaseAuthenticator implements Authenticator {
  2.         public Identity authenticate(String username, String password){
  3.                 ...
  4.         }
  5. }

  6. public class WindowsAuthenticator implements Authenticator {
  7.         public Identity authenticate(String username, String password){
  8.                 ...
  9.         }
  10. }

  11. public class GenericAuthenticator implements Authenticator {
  12.         public Identity authenticate(String username, String password){
  13.                 ...
  14.         }
  15. }
复制代码



再写一个factory class,这样主要程序代码就可以更简洁。
  1. public class AuthenticatorFactory extends FactoryBase {
  2.         @Override
  3.         public Authenticator getAuthenticator(){
  4.                 switch(env.getAuthenticatorType()){
  5.                         case AuthType.DB:
  6.                                 return new DatabaseAuthenticator();
  7.                                 break;
  8.                         case AuthType.Windows:
  9.                                 return new WindowsAuthenticator();
  10.                                 break;
  11.                         default:
  12.                                 return new GenericAuthenticator();
  13.                                 break;
  14.                 }
  15.         }
  16. }
复制代码




最后是主代码里面的登入部分


  1. Authenticator auth = authenticatorFactory.getAuthenticator();
  2. auth.authenticate(myusername, mypassword);
复制代码



这样的情况,开发者鲜少会需要更新主代码,而且可以无限的扩展,例如陆续加入LDAPAuthenticator, FacebookAuthenticator等等。
回复

使用道具 举报


ADVERTISEMENT

发表于 14-1-2012 09:47 PM | 显示全部楼层
回复 28# 苦瓜汤
谢谢, 希望有更多分享
回复

使用道具 举报

发表于 17-1-2012 10:38 AM | 显示全部楼层
回复 25# algorithm

Halo Algo大大,因为Assignment的关系,要做一个cinema booking system..以下是source code..

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

public class CinemaBooking
{
   static final int rows = 7;
   static final int cols = 4;
   static char[][] seats = new char[rows][cols];

   static ArrayList<String> reservedSeats = new ArrayList<String>();

   public static void main(String[] args)
   {
      buildSeats();
      printSeats();

      Scanner scan = new Scanner(System.in);
      String s = "";

      do
      {
         if(!hasAvailableSeats())
         {
            System.out.println("All seats are chosen.  Thank you for using this booking system");
            s="0";
            continue;
         }

         System.out.println("Enter Seat number to reserve or 0 to quit:");
         s = scan.next();

         int row = Integer.parseInt(s.toUpperCase().substring(0, 1));

         if(0==row)
         {
            break;
         }

         char col = s.toUpperCase().charAt(1);

         reserveSeat(row, col);
      } while(!s.equals("0"));
   }

   public static boolean hasAvailableSeats()
   {
      boolean blnHasAvailableSeat = false;
      for(int i =0; ((i < seats.length) && (!blnHasAvailableSeat)); i++)
      {
         for(int j = 0; j < seats.length; j++)
         {
            if('A' == seats[j])
            {
               blnHasAvailableSeat = true;
               break;
            }
         }
      }

      return blnHasAvailableSeat;
   }

   public static void buildSeats()
   {
      char seatLetter = 'A';
      for (int i = 0; i < seats.length; i++)
      {
         for (int j = 0; j < seats.length; j++)
         seats[j] = seatLetter++;
         seatLetter = 'A';
      }
   }

   public static void printSeats()
   {
      System.out.println("Available Seats:");

      for (int i = 0; i < seats.length; i++)
      {
         System.out.print((i + 1) + " ");
         for (int j = 0; j < seats.length; j++)
         System.out.print(seats[j] + " ");
         System.out.println();
      }
   }

   public static void reserveSeat(int row, char col)
   {
      String seatNo=String.valueOf(row)+col;
      if (checkAvailability(seatNo))
      {
         reservedSeats.add(seatNo);
         for (int i = row - 1; i == row - 1; i++)
         {
            for (int j = 0; j < seats.length; j++)
            {
               if (seats[j] == col)
               {
                  seats[j] = 'X';
               }

            }
         }

         System.out.println(" Seat " + seatNo + " is Reserved ");
      }
      else
      {
         System.out.println("Sorry! The Seat "+seatNo+" is NOT available.Please look up for another seat.");
      }

      printSeats();
   }

   public static boolean checkAvailability(String seatNo)
   {
      boolean available = true;
      for(int i=0;i<reservedSeats.size();i++)
      {
         if(reservedSeats.get(i).equalsIgnoreCase(seatNo))
         {
            available = false;
         }
      }
      return available;
   }
}

问题是,我怎么改so that user enter 1E or 8A, 会出现'invalid seat input'呢?
可以教教我吗?
谢谢。。
回复

使用道具 举报

发表于 17-1-2012 11:58 AM | 显示全部楼层
回复 30# rhfh


   用regular expression
回复

使用道具 举报

发表于 17-1-2012 11:26 PM | 显示全部楼层
本帖最后由 algorithm 于 18-1-2012 12:51 AM 编辑

回复 30# rhfh

问题是,我怎么改so that user enter 1E or 8A, 会出现'invalid seat input'呢? <<< 在user key in value 后,validate row和col是不是out of range 或如loonloon0625说的用regular expression。
String seatNo = String.valueOf(row) + col;
!seatNo.matches("[1-7][a-dA-D]")


之前以为你的looping of double array 有问题,原来是因为你贴
的code没有用代码。全部的 [ i ] 都被删掉了。
回复

使用道具 举报

发表于 18-1-2012 11:16 AM | 显示全部楼层
各位老兄
想要<Effective Java>此书,下面是下载link

下载
http://www.filejungle.com/f/Hrdq ... dition.May.2008.rar

如有新发现,不妨这里分享。
祝各位恭喜发财,财源广进,步步高升,
回复

使用道具 举报

发表于 18-1-2012 06:48 PM | 显示全部楼层
回复 33# prolife


红包拿来
回复

使用道具 举报

发表于 19-1-2012 12:11 AM | 显示全部楼层
回复 33# prolife

感谢万分~新年快乐~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 7-11-2025 01:55 AM , Processed in 0.125343 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表