Java中的overload和override

今天去笔试,有一个问题是请写出overload和override的区别,

我愣了半天,最终还是没有写出来,没写出来的理由也是一大堆,

所以今天重温一下.

Overriding a method means that its entire functionality is being replaced.
Overriding is something done in a child class to a method defined in a parent
class.
The methods with the same name and same number of arguments and types but one
is in base class and second is in derived class.
override对应于OO编程的继承特性,意思就是在子类中重写父类中的方法;
满足方法重写的几个条件:
1.方法名相同.
2.子类的访问控制权限不能低于父类被重写方法的访问权限.
3.参数类型,参数个数,参数列表顺序,必须与父类被重写方法的一致.
4.返回值类型相同.

Overloading of methods is a compiler trick to allow you to use the same name
to perform different actions depending on parameters.
The methods with the same name but it differs by types of arguments and number
of arguments.
overload对应于OO编程的多态特性,意思就是在同一个类中同样名称的多个方法, 这多个方法之间的区别在他们的参数列不同。
1.方法名相同
2.参数类型,参数个数,参数列表顺序不相同

package interface1;

import java.util.ArrayList;
import java.util.List;

/**

  • Class demonstrating overloading/overriding issues in Java.
    */
    public class OverloadingExample
    {
    /**
  • Print out a Vehicle’s toString().
  • @param theVehicle The Vehicle whose toString() will be executed.
    */
    public void print(final Vehicle theVehicle)
    {
    System.out.println(“Vehicle: “ + theVehicle);
    }

/**

  • Print out an Automobile’s toString().
  • @param theAutomobile The Automobile whose toString() will be executed.
    */
    public void print(final Automobile theAutomobile)
    {
    System.out.println(“Automobile: “ + theAutomobile);
    }

/**

  • Print out a Car’s toString().
  • @param theCar The Car whose toString() will be executed.
    */
    public void print(final Car theCar)
    {
    System.out.println(“Car: “ + theCar);
    }

/**

  • Main test to be executed.
    */
    public void runTest()
    {
    System.out.println(
    “===== COMPILE-TIME RESOLUTION IS SUFFICIENT FOR OVERLOADING =====”);
    print(new Vehicle());
    print(new Car());
    print(new Automobile());
    System.out.println(
    “===== COMPILE_TIME RESOLUTION NOT SUFFICIENT FOR OVERRIDE=====”);
    final List vehicles = new ArrayList();
    vehicles.add(new Car());
    vehicles.add(new Automobile());
    vehicles.add(new Vehicle());
    for (final Vehicle vehicle : vehicles)
    {
    print(vehicle);
    }
    }

/**

  • Main function to run the test demonstrating Java’s overloading and
  • overriding.
    */
    public static void main(final String[] arguments)
    {
    final OverloadingExample me = new OverloadingExample();
    me.runTest();
    }

/**

  • Parent class with its own toString implementation.
    */
    public static class Vehicle
    {
    @Override
    public String toString() {return “Vehicle”;}
    }

/**

  • Child class with its own toString implementation.
    */
    public static class Automobile extends Vehicle
    {
    @Override
    public String toString() {return “Automobile”;}
    }

/**

  • Grandchild class with its own toString implementation.
    */
    public static class Car extends Automobile
    {
    @Override
    public String toString() {return “Car”;}
    }
    }

opensolaris下找到某个端口属于哪个PID的bash

内容来源:http://sysunconfig.net/unixtips/port2pid

但是做了一些修改。

#!/bin/bash

7-30-2003

find from a port the pid that started the port

line=’————————————————————————-‘
pids=/usr/bin/ps -ef | sed 1d | awk '{print $2}'

Prompt users or use 1st cmdline argument

if [ $# -eq 0 ]
then
echo “Enter port you like to know pid for: “
read ans
else
ans=$1
fi

Check all pids for this port, then list that process

for f in $pids
do
/usr/bin/pfiles $f 2>/dev/null | /usr/gnu/bin/grep -q “port: $ans”
if [ $? -eq 0 ] ; then
echo “$line/nPort: $ans is being used by PID: /c”
/usr/bin/ps -o pid -o args -p $f | sed 1d
fi
done
exit 0

pin、pin2、puk、puk2

pin2码
pin码是由供应商提供,用于Sim卡保密的个人识别码(PersonalIdentificationNumber),在”保密设定”—“开机密码”—“pin”开啟此功能之后,手机开机时需输入pin1码方可使用,即此密码是对Sim卡的锁定。预设值是1234。如果手机密码和pin1码同时使用,则先输入pin1码,后输入手机密码。pin1码3次输入错误之后将被锁死,需用puk来解锁.

pin2码

pin2码是由供应商提供的sim卡另一密码,用于限定拨号等功能的个人识别码,主要用于消除呼叫费用资料、设定通话费的计费币别和计费单位、费用限制功能、限定拨号(”保密设定”—“限定拨号”开啟之后手机只能拨其中设定的号码且不可用电话簿)。pin2码3次输入错误之后将被锁死,需用puk2来解锁。

puk码

puk码是由供应商提供的pin码的解锁码,是一串无规律的数位。puk十次输错,sim卡将永久锁死,只得更换sim卡。

puk2码

puk2码是由供应商提供的pin2码的解锁码,是一串无规律的数位。puk2十次输错,sim卡也将永久锁死,只得
换sim卡。

註:pin、pin2、puk、puk2码均可到供应商处查询,且pin、pin2也可自己修改(须知原来的密码)。

sim卡解锁码

主要用于”锁定sim卡”功能的解锁,防止未知的sim卡未经允许使用本手机,可开啟”锁定sim卡”(”保密设定”—“锁定sim卡”)功能。这样,如果手机中的sim卡未经允许,在开机时就要按照提示输入解锁码。
预设值是00000000。

java来做CLI

使用java来做CLI的话,就要用java来接管terminal,来处理用户的输入和将结果显示给用户,还有可能要去的terminal的宽度和高度

等属性,但是这些在java里坐起来都是力不从心的,那该怎么办了?

只能使用java来调用系统的命令来控制terminal了,有一个现成的jar可以支持java来控制terminal,那就是jline,参考网址是:

http://jline.sourceforge.net/

使用它可以实现命令补全等功能,只要实现Completor类中的complete方法就行了;

将Escape Sequences print到terminal,可以控制terminal的行为;

(Escape Sequences有很多 标准,而且每个terminal emulator实现的方式都不一样,所以要使用的话,

必须在自己所有使用到的terminal emulator进行测试; 比方说putty支持DECTCEM 的实现在Terminal.c中的L2378
)

下面是实现Completro的具体例子:

//buf使用户输入的字符串;

//cursor是光标所处的位置;

//candidates是需要在当前状态下,用户按tab可以候选的值,所放入的列表;

public int complete(final String buf, final int cursor, final List candidates)
{
int length = 0;
String target = null;
int tgtpos = 0;

// Get current buffer to complete
String buffer = (buf == null) ? “” : buf.substring(0, cursor);
// Remove the first blank string
while (buffer.startsWith(this.delimit))
{
buffer = buffer.substring(this.delimit.length());
}

//add your candidate to list

// the index of the completion is always from the beginning of the buffer.
return (candidates.size() == 0) ? (-1) : cursor - tgtpos);
}

下面是实现控制terminal的具体例子(忽略了异常处理):

/**

  • Use escape sequences to control Terminal emulator
  • @param code
    */
    public static void esc(String code)
    {

jlineReader.printString(((char) 27) + code);
jlineReader.flushConsole();
}

/**

  • Move cursor position to home (1,1)
    */
    public static void moveCursorHome()
    {
    esc(“[1;1f”);
    }

/**

  • Move cursor position to footer (height,1)
    */
    public static void moveCursorFooter()
    {
    int height = jlineReader.getTermheight();
    esc(“[“ + height + “;1f”);
    }

/**

  • Erases the current line
    */
    public static void eraseLine()
    {
    esc(“[2K”);
    }

/**

  • Hides the cursor
    */
    public static void hideCursor()
    {
    esc(“[?25l”);
    }

/**

  • Shows the cursor
    */
    public static void showCursor()
    {
    esc(“[?25h”);
    }

/**

  • Shows the cursor
    */
    public static void resetTerminal()
    {
    esc(“c”);
    }

/**

*这个方法是相当于翻页;相当于linux中的clear命令

*/

public static void clearScreen()
{

jlineReader.clearScreen();
}

/**

*这个方法是将当前页页,清空,同时将光标移动到当前页的开始位置;注意它和clearScreen不同;

*/

public static void redrawScreen()
{
int width = jlineReader.getTermwidth();
int height = jlineReader.getTermheight();

int count = width * height;
char[] bufs = new char[count];
Arrays.fill(bufs, ‘ ‘);
try
{
jlineReader.printString(new String(bufs));
jlineReader.flushConsole();
}
catch (IOException ex)
{
logger.error(ex);
}
moveCursorHome();
}

opensolaris下如何取得系统启动时间

因为同uptime命令获得的时间没有秒,所以只能自己写一个了;

具体的实现可参见:

http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/w/w.c

总的思路就是读取UTMPX_FILE,取出其中ut_type为BOOT_TIME用户的ut_xtime,

然后用now减去ut_xtime,就是系统启动时间了;

static long getSysUpTime(void)
{
struct stat sbuf;
size_t size = 0;

struct utmpx ut;
struct utmpx
utmpbegin;
struct utmpx utmpend;
struct utmpx
utp;

time_t now = 0;
time_t uptime = 0;

//read the UTMP_FILE (contains information about each logged in user)
if (stat(UTMPX_FILE, &sbuf) == -1) {
printf(“read /etc/utmpx file error.”);
exit(1);
}

//get the entry number in UTMPX_FILE;
int entries = sbuf.st_size / sizeof (struct futmpx);
size = sizeof (struct utmpx) * entries;
//malloc memory to store info from UTMPX_FILE;
if ((ut = malloc(size)) == NULL) {
printf(“malloc error when create ut info;”);
exit(1);
}

//prepare to iterator UTMPX_FILE;
(void) utmpxname(UTMPX_FILE);

//iterator UTMPX_FILE

utmpbegin = ut;
utmpend = (struct utmpx )((char )utmpbegin + size);

setutxent();
while ((ut < utmpend) && ((utp = getutxent()) != NULL))
(void) memcpy(ut++, utp, sizeof (*ut));
endutxent();

//get current time;
(void) time(&now);
//iterator utmp entry in memory;
for (ut = utmpbegin; ut < utmpend; ut++)
{
if (ut->ut_type == BOOT_TIME)
{
//this entry is boot_time, so get uptime with now;
uptime = now - ut->ut_xtime;
}
}

printf(“%f”, uptime);
//time is sencond 100;
return uptime
100;
}

snmp的VACM使用

呵呵,废话不多说,直接用例子说明,假如我的snmpd.conf文件如下:

首先将v1和v2c community名字为public的映射到security name,

其实想到与给public 起了一个v3的名字;

#First, map the community name (COMMUNITY) into a security name

sec.name source community

com2sec my_sec default public

然后将security name映射到group中;在v3的情况下,v3的用户名就是security name;

Second, map the security names into group names:

sec.model sec.name

group my_grp v1 my_sec
group my_grp v2c my_sec

group my_usm_grp usm test # SNMPv3 username == sec.name

创建访问规则;

Third, create a view for us to let the groups have rights to:

incl/excl subtree mask

#名字叫做all的view,就代表.1以下的所有节点;
view all included .1

#名字叫做mini_view的view,只代表sysUpTime;

view mini_view excluded .1
view mini_view included sysUpTime.0

#名字叫做if_view的view,代表sysUpTime和ifTable;

view if_view excluded .1
view if_view included sysUpTime.0
view if_view included ifTable

最后给不同的group设置不同的访问节点;

Finally, grant the groups access to their views:

#sec.level {noauth|auth|priv}

context sec.model sec.level match read write notify

#group my_grp中的用户可以访问所有的节点
access my_grp “” any noauth exact all none none

#group my_usm_grp中的v3用户,如果是通过noauth过来的,只能访问sysUpTime节点;

access my_usm_grp “” usm noauth exact mini_view none none

#group my_usm_grp中的v3用户,如果是通过auth过来的,可以访问sysUpTime和ifTalbe节点;
access my_usm_grp “” usm auth exact if_view none none

#group my_usm_grp中的v3用户,如果是通过priv过来的,可以访问所有的节点;
access my_usm_grp “” usm priv exact all none none

android下载不了的问题

不知道问什么下载android时,老是下载不了,所以才有此文,但是下面的方法可以使用的

前提是你可以正常访问google英文版;

首先在以下URL中查找自己需要的文件

https://dl-ssl.google.com/android/repository/repository.xml

然后将自己需要的文件方到https://dl-ssl.google.com/android/repository/后,用浏览器下载;

例子:

假如我要下载android-2.2_r02-windows.zip,在https://dl-
ssl.google.com/android/repository/repository.xml

查找看有没有,有的话,构建URL,如下:

https://dl-ssl.google.com/android/repository/android-2.2_r02-windows.zip,下载;

如果没有的话,也可以构建URl试着下载,因为repository.xml里面只是最新的组件。