例12-1 一个简单的Java程序
//文件名位JavaExample.java,文件名与公共类名完全相同
public class JavaExample{
public static void main(String[] args){
System.out.println("A java program example!");
}
}
例12-2 使用Math类库程序实例。
//此类定义在TestMath.java文件中
public class TestMath{
public static void main(String[] args){
//计算一个面积为360平方英尺的圆的半径,半径单位为应粗
double radios=0.0;//初始化半径的值为0
double circleArea=360.0;//圆的面积为360平方英尺
radius=Math.sqrt(circleArea/Math.PI);//计算圆的半径
System.out.println(circleArea+"平方英尺的圆的半径为:\n"+radius+"英寸");
}
}
例12-3 逻辑运算符&&、&使用举例。
public class AndExample{
public static void main(String args[]){
boolean aValue=false;
boolean bValue=false;
int i=1,j=2,z=0;
aValue=(j<2)&&(z=i+j)>j);
System.out.println("aValue="+aValue);
System.out.println("z="+z);
bValue=(j<2)&((z=i+j)>j);
System.out.println("bValue="+bValue);
System.out.println("z="+z);
}
}
例12-4 字符串运算举例。
public class MatchStrings{
public static void main(String[] args){
String string1="I am a ";
String string2="programmer!";
String string3="I am a programmer!";
string1+=string2;//现在string1和string3所引用对象的内容相同
System.out.println("string3 is:"+string3);
System.out.println("string1 is:"+string1);
if(string1==string3)//测试是否引用同一字符串
System.out.println("string1==string3 is true. "+"string1和string3引用同一字符串");
else
System.out.println(string1==string3 is false. "+"string1和string3引用不同字符串");
if(string1.equals(string3))//测试内容是否相同
System.out.pringln("string1 equals string3 is true.");
else
System.out.pringln("string1 equals string3 is false.");
string3=string1;//string3与string1引用同一字符串
if(string1==string3)
System.out.pringln("string1==string3 is true. "+"string1和string3引用相同的字符串");
else
System.out.println("string1==string3 is false. "+"string1和string3引用不同的字符串");
}
}
例12-5 类的定义和使用示例
public class myClass{
public static void main(String args[]){
System.out.pringln(test.geti());
test tc=null;
System.out.pringln(tc.geti());//可以,但不推荐
System.out.pringln(tc.getj());//错误的调用,因为tc还未引用对象
tc=new test();
System.out.pringln(tc.geti());//可以,但不推荐
System.out.pringln(tc.getj());
}
}
class test{
static int i=0;//定义一个静态变量i
int j=3;//定义一个实例变量j
public static int geti(){//定义一个静态方法
return i;
}
public int getj(){//定义一个示例方法
reutrn j;
}
}
例12-6 下面的代码中包含了静态变量、实例变量、静态初始化块、非静态初始化块以及构造方法。
public class Initialization{
public static void main(String[] args){
TryInit testObj=new TryInit();
}
}
class TryInit{
static int iStaticVariable=-1;//通过声明初始化静态变量
static int jStaticVariable;
int iInstanceVariable=1;//通过声明初始化非静态变量
int jInstanceVariable;
{//非静态初始化块
jInstanceVariable=2;
System.out.println("iInstanceVariable="+iInstanceVariable);
System.out.println("jInstanceVariable="+jInstanceVariable);
System.out.println("在非静态初始化块中!");
}
static{//静态初始化块
jStaticVarable=-2;
System.out.println("iStaticVariable="+iStaticVariable);
System.out.println("jStaticVariable="+jStaticVariable);
System.out.println("在非静态初始化块中!");
}
public TryInit(int i,int j){//带参数的构造方法
iInstanceVariable=i;
jInstanceVariable=j;
System.out.println("在狗仔方法TryInit(int i,int j)中!");
}
public TryInit(){
this(3,4);
System.out.println("iInstanceVariable="+iInstanceVariable);
System.out.pringln("jInstanceVariable="+jInstanceVariable);
}
}
例12-7 使用继承的一个简单例子。
public class extExample{
public static void main(String[] args){
subClass s=new subClass();
System.out.println(s.getiValue());
System.out.println(s.getjValue());
}
}
class superClass{
int i=5;
public int getiValue(){
return i;
}
}
class subClass extends superClass(){
int j=-3;
public int getjValue(){
return j;
}
}
例12-8 子类覆盖父类方法
public class extExample{
public static void main(String[] args){
superClass s=new subClass();
System.out.println(s.getValue());
s=new superClass();
System.out.println(s.getValue());
}
}
class superClass{
int i=5;
public int getValue(){
return i;
}
}
class subClass extends superClass{
int j;
public subClass(){
this(-3);
}
public subClass(int i){this.j=i;}
public int getValue(){
return j;
}
}
例12-9 抽象类的定义与使用。
public class extExample{
public static void main(String[] args){
superClass s=new subClass();
System.out.println(s.getValue());
System.out.println(s.getSum());
}
}
abstract class superClass{
int i=5;
public int getValue(){
return i;
}
public abstract int getSum();
}
class subClass extends superClass{
int j;
public subClass(){
this(-3);
}
public subClass(int i){this.j=i;}
public int getValue(){
return j;
}
public int getSum(){
return i+j;
}
}
例12-10 以下程序的功能是计算三角形、举行和正方形的面积并输出。
public class AreaTest{
public static void main(String[] args){
Figure[] figures={
new Triangle(2,3,3),new Rectanle(5,8),new Square(5)
};
for(int i=0;i<figures.length;i++){
System.out.println(figures[i]+"area="+figures[i].getArea());
}
}
}
public abstract class Figure{//抽象类
public abstract double getArea();//抽象方法
}
public class Rectangle extends Figure{
double height;
double width;
public Rectangle(double height,double width){
this.height=height;
this.width=width;
}
public String toString(){
return "Rectangle:height="+height+",width="+width+":";
}
public double getArea(){
return height*width;
}
}
public class Square extends Rectangle{
public Square(double width){
super(width,width);
}
public String toString(){
return "Square:width="+width+":";
}
}
public class Triangle extends Figure{
double la;
double lb;
double lc;
public Triangle(double la,double lb,double lc){
this.la=la;this.lb=lb;this.lc=lc;
}
public String toString(){
return "Triangle:sides="la+","+lb+","+lac+":";
}
public double getArea(){
double s=(la+lb+lc)/2.0;
return Math.sqrt(s.(s-la)*(s-lb)*(s-lc));
}
}
//文件名位JavaExample.java,文件名与公共类名完全相同
public class JavaExample{
public static void main(String[] args){
System.out.println("A java program example!");
}
}
例12-2 使用Math类库程序实例。
//此类定义在TestMath.java文件中
public class TestMath{
public static void main(String[] args){
//计算一个面积为360平方英尺的圆的半径,半径单位为应粗
double radios=0.0;//初始化半径的值为0
double circleArea=360.0;//圆的面积为360平方英尺
radius=Math.sqrt(circleArea/Math.PI);//计算圆的半径
System.out.println(circleArea+"平方英尺的圆的半径为:\n"+radius+"英寸");
}
}
例12-3 逻辑运算符&&、&使用举例。
public class AndExample{
public static void main(String args[]){
boolean aValue=false;
boolean bValue=false;
int i=1,j=2,z=0;
aValue=(j<2)&&(z=i+j)>j);
System.out.println("aValue="+aValue);
System.out.println("z="+z);
bValue=(j<2)&((z=i+j)>j);
System.out.println("bValue="+bValue);
System.out.println("z="+z);
}
}
例12-4 字符串运算举例。
public class MatchStrings{
public static void main(String[] args){
String string1="I am a ";
String string2="programmer!";
String string3="I am a programmer!";
string1+=string2;//现在string1和string3所引用对象的内容相同
System.out.println("string3 is:"+string3);
System.out.println("string1 is:"+string1);
if(string1==string3)//测试是否引用同一字符串
System.out.println("string1==string3 is true. "+"string1和string3引用同一字符串");
else
System.out.println(string1==string3 is false. "+"string1和string3引用不同字符串");
if(string1.equals(string3))//测试内容是否相同
System.out.pringln("string1 equals string3 is true.");
else
System.out.pringln("string1 equals string3 is false.");
string3=string1;//string3与string1引用同一字符串
if(string1==string3)
System.out.pringln("string1==string3 is true. "+"string1和string3引用相同的字符串");
else
System.out.println("string1==string3 is false. "+"string1和string3引用不同的字符串");
}
}
例12-5 类的定义和使用示例
public class myClass{
public static void main(String args[]){
System.out.pringln(test.geti());
test tc=null;
System.out.pringln(tc.geti());//可以,但不推荐
System.out.pringln(tc.getj());//错误的调用,因为tc还未引用对象
tc=new test();
System.out.pringln(tc.geti());//可以,但不推荐
System.out.pringln(tc.getj());
}
}
class test{
static int i=0;//定义一个静态变量i
int j=3;//定义一个实例变量j
public static int geti(){//定义一个静态方法
return i;
}
public int getj(){//定义一个示例方法
reutrn j;
}
}
例12-6 下面的代码中包含了静态变量、实例变量、静态初始化块、非静态初始化块以及构造方法。
public class Initialization{
public static void main(String[] args){
TryInit testObj=new TryInit();
}
}
class TryInit{
static int iStaticVariable=-1;//通过声明初始化静态变量
static int jStaticVariable;
int iInstanceVariable=1;//通过声明初始化非静态变量
int jInstanceVariable;
{//非静态初始化块
jInstanceVariable=2;
System.out.println("iInstanceVariable="+iInstanceVariable);
System.out.println("jInstanceVariable="+jInstanceVariable);
System.out.println("在非静态初始化块中!");
}
static{//静态初始化块
jStaticVarable=-2;
System.out.println("iStaticVariable="+iStaticVariable);
System.out.println("jStaticVariable="+jStaticVariable);
System.out.println("在非静态初始化块中!");
}
public TryInit(int i,int j){//带参数的构造方法
iInstanceVariable=i;
jInstanceVariable=j;
System.out.println("在狗仔方法TryInit(int i,int j)中!");
}
public TryInit(){
this(3,4);
System.out.println("iInstanceVariable="+iInstanceVariable);
System.out.pringln("jInstanceVariable="+jInstanceVariable);
}
}
例12-7 使用继承的一个简单例子。
public class extExample{
public static void main(String[] args){
subClass s=new subClass();
System.out.println(s.getiValue());
System.out.println(s.getjValue());
}
}
class superClass{
int i=5;
public int getiValue(){
return i;
}
}
class subClass extends superClass(){
int j=-3;
public int getjValue(){
return j;
}
}
例12-8 子类覆盖父类方法
public class extExample{
public static void main(String[] args){
superClass s=new subClass();
System.out.println(s.getValue());
s=new superClass();
System.out.println(s.getValue());
}
}
class superClass{
int i=5;
public int getValue(){
return i;
}
}
class subClass extends superClass{
int j;
public subClass(){
this(-3);
}
public subClass(int i){this.j=i;}
public int getValue(){
return j;
}
}
例12-9 抽象类的定义与使用。
public class extExample{
public static void main(String[] args){
superClass s=new subClass();
System.out.println(s.getValue());
System.out.println(s.getSum());
}
}
abstract class superClass{
int i=5;
public int getValue(){
return i;
}
public abstract int getSum();
}
class subClass extends superClass{
int j;
public subClass(){
this(-3);
}
public subClass(int i){this.j=i;}
public int getValue(){
return j;
}
public int getSum(){
return i+j;
}
}
例12-10 以下程序的功能是计算三角形、举行和正方形的面积并输出。
public class AreaTest{
public static void main(String[] args){
Figure[] figures={
new Triangle(2,3,3),new Rectanle(5,8),new Square(5)
};
for(int i=0;i<figures.length;i++){
System.out.println(figures[i]+"area="+figures[i].getArea());
}
}
}
public abstract class Figure{//抽象类
public abstract double getArea();//抽象方法
}
public class Rectangle extends Figure{
double height;
double width;
public Rectangle(double height,double width){
this.height=height;
this.width=width;
}
public String toString(){
return "Rectangle:height="+height+",width="+width+":";
}
public double getArea(){
return height*width;
}
}
public class Square extends Rectangle{
public Square(double width){
super(width,width);
}
public String toString(){
return "Square:width="+width+":";
}
}
public class Triangle extends Figure{
double la;
double lb;
double lc;
public Triangle(double la,double lb,double lc){
this.la=la;this.lb=lb;this.lc=lc;
}
public String toString(){
return "Triangle:sides="la+","+lb+","+lac+":";
}
public double getArea(){
double s=(la+lb+lc)/2.0;
return Math.sqrt(s.(s-la)*(s-lb)*(s-lc));
}
}