|
|
Effective Java - General Programming
[复制链接]
|
|
|

楼主 |
发表于 6-2-2011 12:01 AM
|
显示全部楼层
伯乐希望马儿好(code quality),也希望马儿不吃草(code得快),大大有什么对策呢?
User. 发表于 5-2-2011 10:45 PM 
好的马儿也会[选]自己的伯乐的.. |
|
|
|
|
|
|
|
|
|
|
发表于 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。
- private class Dog{
- @Override
- public String toString()
- {
- return "A 4 legged animal. Man's best friend.";
- }
- }
复制代码- System.out.println(new Dog().toString());
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 17-12-2011 01:31 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 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..两个星期后就要交了... |
|
|
|
|
|
|
|
|
|
|
发表于 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的话就非常简洁,而且扩展性高。
- /**
- * Authenticator Interface
- * @author Kugua
- */
- public interface Authenticator {
- /**
- * Authenticate user based on user's environment settings
- * @param username User's username
- * @param password User's plain-text password
- * @return Identity object of the authenticated user. null object will be returned if authentication failed.
- */
- public Identity authenticate(String username, String password);
- }
复制代码
接下来是三种implementations
- public class DatabaseAuthenticator implements Authenticator {
- public Identity authenticate(String username, String password){
- ...
- }
- }
- public class WindowsAuthenticator implements Authenticator {
- public Identity authenticate(String username, String password){
- ...
- }
- }
- public class GenericAuthenticator implements Authenticator {
- public Identity authenticate(String username, String password){
- ...
- }
- }
复制代码
再写一个factory class,这样主要程序代码就可以更简洁。
- public class AuthenticatorFactory extends FactoryBase {
- @Override
- public Authenticator getAuthenticator(){
- switch(env.getAuthenticatorType()){
- case AuthType.DB:
- return new DatabaseAuthenticator();
- break;
- case AuthType.Windows:
- return new WindowsAuthenticator();
- break;
- default:
- return new GenericAuthenticator();
- break;
- }
- }
- }
复制代码
最后是主代码里面的登入部分
- Authenticator auth = authenticatorFactory.getAuthenticator();
- auth.authenticate(myusername, mypassword);
复制代码
这样的情况,开发者鲜少会需要更新主代码,而且可以无限的扩展,例如陆续加入LDAPAuthenticator, FacebookAuthenticator等等。 |
|
|
|
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 18-1-2012 06:48 PM
|
显示全部楼层
回复 33# prolife
红包拿来 |
|
|
|
|
|
|
|
|
|
|
发表于 19-1-2012 12:11 AM
|
显示全部楼层
回复 33# prolife
感谢万分~新年快乐~ |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|