Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19911981
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Java

2009-07-03 16:35:25

操作对象

2009-07-03

磁针石:ouyangchongwu#gmail.com



New中使用不同个数的参数:

import java.util.StringTokenizer;

 

class TokenTester {

 

    public static void main(String[] arguments) {

        StringTokenizer st1, st2;

 

        String quote1 = "VIZY 3 -1/16";

        st1 = new StringTokenizer(quote1);

        System.out.println("Token 1: " + st1.nextToken());

        System.out.println("Token 2: " + st1.nextToken());

        System.out.println("Token 3: " + st1.nextToken());

 

        String quote2 = "NPLI@9 27/32@3/32";

        st2 = new StringTokenizer(quote2, "@");

        System.out.println("\nToken 1: " + st2.nextToken());

        System.out.println("Token 2: " + st2.nextToken());

        System.out.println("Token 3: " + st2.nextToken());

    }

}

              

执行结果:

D:\java\9433-source-code\chapter3>java TokenTester

Token 1: VIZY

Token 2: 3

Token 3: -1/16

 

Token 1: NPLI

Token 2: 9 27/32

Token 3: 3/32

 

改变实例变量:本例使用了import java.awt.Point;,暂略。

类变量:

class FamilyMember {

static String surname = “Mendoza”;

String name;

int age;

}

建议修改是用类名来引用。

 

方法的调用:字符串检查实例。

class StringChecker {

 

    public static void main(String[] arguments) {

        String str = "Nobody ever went broke by buying IBM";

        System.out.println("The string is: " + str);

        System.out.println("Length of this string: "

            + str.length());

        System.out.println("The character at position 5: "

            + str.charAt(5));

        System.out.println("The substring from 26 to 32: "

            + str.substring(26, 32));

        System.out.println("The index of the character v: "

            + str.indexOf('v'));

        System.out.println("The index of the beginning of the "

            + "substring \"IBM\": " + str.indexOf("IBM"));

        System.out.println("The string in upper case: "

            + str.toUpperCase());

    }

}

 

执行结果:

D:\java\9433-source-code\chapter3>java StringChecker

The string is: Nobody ever went broke by buying IBM

Length of this string: 36

The character at position 5: y

The substring from 26 to 32: buying

The index of the character v: 8

The index of the beginning of the substring "IBM": 33

The string in upper case: NOBODY EVER WENT BROKE BY BUYING IBM

 

格式化输出:

int accountBalance = 5005;

System.out.format(“Balance: $%,d%n”, accountBalance);

 

double pi = Math.PI;

System.out.format(“%.11f%n”, pi);

参考:

 

int result = (int)(x / y);

 

类的类型转换:Employee是父类,VicePresident是子类。

Employee emp = new Employee();

VicePresident veep = new VicePresident();

emp = veep; // no cast needed for upward use

veep = (VicePresident)emp; // must cast explicitly

个人感觉类似于补全父类中的信息。

每种基本类型都有对应的类,比如doule对应的类为Double

 

Integer dataCount = new Integer(7801);

int newCount = dataCount.intValue(); // returns 7801

String pennsylvania = “65000”;

int penn = Integer.parseInt(pennsylvania);

 

自动转换:

Float f1 = new Float(12.5F);

Float f2 = new Float(27.2F);

System.out.println(“Lower number: “ + Math.min(f1, f2));

 

 

对象比较可以使用:==!=,判断是否指向同一对象。

class EqualsTester {

    public static void main(String[] arguments) {

        String str1, str2;

        str1 = "Free the bound periodicals.";

        str2 = str1;

 

        System.out.println("String1: " + str1);

        System.out.println("String2: " + str2);

        System.out.println("Same object? " + (str1 == str2));

 

        str2 = new String(str1);

 

        System.out.println("String1: " + str1);

        System.out.println("String2: " + str2);

        System.out.println("Same object? " + (str1 == str2));

        System.out.println("Same value? " + str1.equals(str2));

    }

}

 

    此处必须创建新的变量,不能使用字面变量。字面变量在java中有优化,会自动指向str1

 

 

D:\java\9433-source-code\chapter3>JAVA EqualsTeste

String1: Free the bound periodicals.

String2: Free the bound periodicals.

Same object? true

String1: Free the bound periodicals.

String2: Free the bound periodicals.

Same object? false

Same value? True

 

获取类名:

String name = key.getClass().getName();

getClassObject类定义。

检查是否其实例或者接口:

boolean check1 = “Texas” instanceof String // true

Point pt = new Point(10, 10);

boolean check2 = pt instanceof String // false

阅读(2230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~