coderLucas

Miracles happen every day.

Java:基础

Hello World

//HelloWorld.java文件

public class HelloWorld

{

public static void main(String[] args)

{

        System.out.println(“Hello World!”);

}

}

 

编译运行

javac HelloWorld.java 编译生成HelloWorld.class

java HelloWorld 运行main方法

 

变量

 

  存储大小 例值 注释
byte 1byte 3 字节
int 4bytes 3 整数
short 2bytes 3 短整数
long 8bytes 3 长整数
float 4bytes 1.2 单精度浮点数
double 8bytes 1.2 双精度浮点数
char 2bytes ‘a’ 字符
boolean 1bit true 布尔值

 

数组

int[ ] a;

int[ ] a = new int[100];

int[ ] a = new int[ ] {1,3,5,7,9};

使用int[i],i从0开始来调用i下标元素。

位运算

^ xor异或

~ nor同或

数据成员初始化

 

基本类型的数据成员的默认初始值:

数值型: 0

布尔值: false

其他类型: null

构造器与方法重载

构造优先级:构建方法 > 显式初始值 > 默认初始值

 

封装

在一个.java文件中,有且只能有一个类带有public关键字,从任意其他类中,我们都可以直接调用该类。

 

interface

在interface中,我们

  • 不需要定义方法的主体
  • 不需要说明方法的可见性,interface中的方法默认为public。

 

implements关键字来实施interface。一旦在类中实施了某个interface,必须在该类中定义interface的所有方法(addWater()和drinkWater())。类中的方法需要与interface中的方法原型相符。否则,Java将报错。

例子:

interface Cup {
    void addWater(int w);
    void drinkWater(int w);
}

class MusicCup implements Cup 
{
    public void addWater(int w) 
    {
        this.water = this.water + w;
    }

    public void drinkWater(int w)
    {
        this.water = this.water - w;
    }

    private int water = 0;
}
实现多个接口例子:
class MusicCup implements MusicPlayer, Cup
{
    public void addWater(int w) 
    {
        this.water = this.water + w;
    }

    public void drinkWater(int w)
    {
        this.water = this.water - w;
    }

    public void play()
    {
        System.out.println("la...la...la");
    }

    private int water = 0;
}
 

组合has-a

image

class Battery 
{
    public void chargeBattery(double p)
    {
        if (this.power < 1.) {
            this.power = this.power + p;
        }
    }

    public boolean useBattery(double p)
    {
        if (this.power >= p) {
            this.power = this.power - p;
            return true;
        }
        else {
            this.power = 0.0;
            return false;
        }
    }

    private double power = 0.0;
}

class Torch
{
    /** 
     * 10% power per hour use
     * warning when out of power
     */
    public void turnOn(int hours)
    {
        boolean usable;
        usable = this.theBattery.useBattery( hours*0.1 );
        if (usable != true) {
            System.out.println("No more usable, must charge!");
        }
    }

    /**
     * 20% power per hour charge
     */
    public void charge(int hours)
    {
        this.theBattery.chargeBattery( hours*0.2 );
    }

    /**
     * composition
     */
    private Battery theBattery = new Battery();
}
 

包的建立

在Java程序的开始加入package,

类是由完整的路径识别的,所以不同的包中可以有同名的类,Java不会混淆。

 

package com.vamei.society;//表示该程序在com.vamei.society包中

public class Human //将类放入包com.vamei.society中
{
    /**
     * constructor
     */
    public Human(int h)
    {
        this.height = h;
        System.out.println("I'm born");
    }

    /**
     * accessor
     */
    public int getHeight()
    {
        return this.height;
    }

    /**
     * mutator
     */
    public void growHeight(int h)
    {
        this.height = this.height + h;
    }

    private int height;
}

一个Java程序中只能有一个public的类,该类要与.java文件同名。

一个类可以没有public关键字,它实际上也表示一种权限: 该类在它所在的包中可见。也就是说,包中的其他Java程序可以访问该类。这是Java中的默认访问权限。对象的成员也可以是默认权限(包中可见)。

 

包的调用

将Human.java编译的Human.class放入相应的文件夹

import com.vamei.society.*;//用于识别路径,*表示引入society文件夹下的所有类。

public class TestAgain
{
    public static void main(String[] args)
    {
        Human aPerson = new Human(180);//也可以提供类的完整的路径com.vamei.society.Human
        System.out.println(aPerson.getHeight());
    }

}
posted @ 2014-05-15 10:32  lucas hsueh  阅读(152)  评论(0编辑  收藏  举报