java程序设计--孙鑫java无难事Lesson2《位运算及面向对象程序设计》
1.位运算
Java中有三个移位运算符左移:<<
带符号右移:>>
无符号右移:>>>这部分内容可参见任何一本计算机操作系统或者计算机导论类书籍。
位运算测试程序如下:
//***********************************
- class Test
- {
- public static void main(String[] args)
- {
- int i=0xFFFFFFFF;//-1
- int c=i<<2;
- int d=i>>2;//有符号右移
- int e=i>>>2;//无符号右移
- System.out.println(i);//-1
- System.out.println(c);//-4
- System.out.println(Integer.toHexString(c));//有符号左移 fffffffc
- System.out.println(Integer.toHexString(d));//有符号右移 ffffffff
- System.out.println(Integer.toHexString(e));//无符号右移 3fffffff
- }
- }
class Test
{
public static void main(String[] args)
{
int i=0xFFFFFFFF;//-1
int c=i<<2;
int d=i>>2;//有符号右移
int e=i>>>2;//无符号右移
System.out.println(i);//-1
System.out.println(c);//-4
System.out.println(Integer.toHexString(c));//有符号左移 fffffffc
System.out.println(Integer.toHexString(d));//有符号右移 ffffffff
System.out.println(Integer.toHexString(e));//无符号右移 3fffffff
}
}
//***********************************
2.面向对象的概念
(1)概念
(2)面向对象解决问题-计算矩形周长和面积
Rectangle类测试程序如下:
//***********************************
- <SPAN style="FONT-SIZE: 18px">class Rectangle
- {
- int w,l;
- int perimeter()
- {
- return 2*(l+w);
- }
- int area()
- {
- return l*w;
- }
- public static void main(String[] args)
- {
- Rectangle rect1=new Rectangle();
- Rectangle rect2=new Rectangle();
- rect1.l=10;
- rect1.w=5;
- rect2.l=4;
- rect2.w=6;
- System.out.println("the perimeter of rect1 is "+rect1.perimeter());//the perimeter of rect1 is 30
- System.out.println("the area of rect2 is "+rect2.area());//the area of rect2 is 24
- }
- }</SPAN>
class Rectangle
{
int w,l;
int perimeter()
{
return 2*(l+w);
}
int area()
{
return l*w;
}
public static void main(String[] args)
{
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.l=10;
rect1.w=5;
rect2.l=4;
rect2.w=6;
System.out.println("the perimeter of rect1 is "+rect1.perimeter());//the perimeter of rect1 is 30
System.out.println("the area of rect2 is "+rect2.area());//the area of rect2 is 24
}
}
//***********************************
3.构造函数
(1)默认构造函数,查看一个类的函数和成员列表通过如下指令:F:\java\JavaLesson\Lesson2>javap Point
//***********************************
获得信息为
//***********************************
Compiled from "Point.java"
class Point {
int x;
int y;
Point();//默认构造函数
void output();
public static void main(java.lang.String[]);
}
//***********************************
(2)默认构造函数提供的默认值
数值型 0
boolean false
char '\0'
对象 null
(3)子类不能继承父类的构造函数
子类的构造函数会默认调用父类的不带参数的构造函数,super();如果父类不存在不带参数的构造函数则发生错误,错误信息如下:
Animal.java:33: 错误: 无法将类 Animal中的构造器 Animal应用到给定类型;
需要: int,int
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同
(4)this调用本类其他构造函数时必须为第一条语句,否则错误信息如下:
Point.java:9: 错误: 对this的调用必须是构造器中的第一个语句this(1,1);
4.new 关键字的作用
a.为对象分配内存空间
b.引起对象构造方法的调用
c.为对象返回一个引用
5.static静态方法与静态成员变量
(1)概述
静态方法和静态变量是属于某一个类,而不属于类的对象。静态方法和静态变量的引用直接通过类名引用。
在静态方法中不能调用非静态的方法和引用非静态的成员变量。反之,则可以。
可以用类的对象obj去调用静态的方法method(),如:obj.method()。
(2)静态成员函数中访问非静态成员变量引起错误,错误代码及信息如下:
static void output()
{
System.out.println("output called!");
System.out.println(x);
System.out.println(y);
}
Point.java:29: 错误: 无法从静态上下文中引用非静态 变量 x
System.out.println(x);
^
Point.java:30: 错误: 无法从静态上下文中引用非静态 变量 y
System.out.println(y);
(3)常量的初始化
常量由关键字final定义,必须赋初值,定义为非静态时可以放在构造函数中,
而为了避免每个对象的拷贝该数据则可以声明为静态的,可以共享的数据,此时必须在定义时初始化.
定义 final double PI;而没有初始化时,错误信息如下
Point.java:14: 错误: 可能尚未初始化变量PI
静态常量声明形如:static final double PI=3.1415926;
Point类测试程序如下:
//***********************************
- <SPAN style="FONT-SIZE: 18px">class Point
- {
- int x,y;//对象变量
- static int z;//类变量属于类本身,不属于某个对象
- //常量必须赋初值 3.1415926 定义为非静态时可以放在构造函数中
- //定义为静态的 共享该数据 避免每个对象的拷贝 此时必须在定义时初始化
- static final double PI=3.1415926;//静态常量必须再此初始化
- Point()
- {
- //PI=3.1415926;//ok 非静态常量可以这里初始化
- this(1,1);//调用带参数的构造函数 必须为第一条语句
- }
- Point(int a,int b)
- {
- //PI=3.1415926;//ok 非静态常量可以这里初始化
- x=a;
- y=b;
- }
- void output(int x,int y)
- {
- this.x=x;
- this.y=y;//内部成员变量被隐藏 这里只是形参可见 因此采用this
- }
- /*
- //实例方法
- void output()
- {
- System.out.println(x);
- System.out.println(y);
- }
- */
- /*
- //静态方法
- static void output()
- {
- System.out.println("output called!");
- System.out.println(x);//error 静态方法引用非静态成员变量
- System.out.println(y);//error
- }*/
- static void output()
- {
- System.out.println("output called!");
- System.out.println(z);
- }
- public static void main(String[] args)
- { /*
- Point pt;
- pt=new Point();
- pt.x=10;
- pt.y=10;
- pt.output();//输出类的成员变量信息
- */
- /*
- Point pt;
- pt=new Point(3,3);
- pt.output(5,5);
- pt.output();//result 3,3 使用this解决函数参数和成员变量的重名问题
- /*
- Point pt;
- pt=new Point();
- pt.output();//result 1,1 默认不带参数构造函数可以调用带参数的构造函数
- */
- /*
- Point pt;
- pt=new Point();
- Point pt2=new Point();
- pt.x=6;
- pt2.x=7;
- System.out.println(pt.x);
- System.out.println(pt2.x);//方法只有一份拷贝 而数据成员却有两份不同的拷贝
- */
- /*
- //静态函数 static void output() 的调用
- Point.output();//直接通过类调用
- pt=new Point();
- pt.output();//对象调用静态方法实质还是通过类调用
- */
- /*
- //静态变量z的共享
- Point pt1=new Point();
- Point pt2=new Point();
- pt1.z=6;
- pt2.z=7;//pt1和pt2共享 z变量
- System.out.println(pt1.z);// result 7
- System.out.println(pt2.z);// result 7
- */
- }
- }</SPAN>
class Point
{
int x,y;//对象变量
static int z;//类变量属于类本身,不属于某个对象
//常量必须赋初值 3.1415926 定义为非静态时可以放在构造函数中
//定义为静态的 共享该数据 避免每个对象的拷贝 此时必须在定义时初始化
static final double PI=3.1415926;//静态常量必须再此初始化
Point()
{
//PI=3.1415926;//ok 非静态常量可以这里初始化
this(1,1);//调用带参数的构造函数 必须为第一条语句
}
Point(int a,int b)
{
//PI=3.1415926;//ok 非静态常量可以这里初始化
x=a;
y=b;
}
void output(int x,int y)
{
this.x=x;
this.y=y;//内部成员变量被隐藏 这里只是形参可见 因此采用this
}
/*
//实例方法
void output()
{
System.out.println(x);
System.out.println(y);
}
*/
/*
//静态方法
static void output()
{
System.out.println("output called!");
System.out.println(x);//error 静态方法引用非静态成员变量
System.out.println(y);//error
}*/
static void output()
{
System.out.println("output called!");
System.out.println(z);
}
public static void main(String[] args)
{ /*
Point pt;
pt=new Point();
pt.x=10;
pt.y=10;
pt.output();//输出类的成员变量信息
*/
/*
Point pt;
pt=new Point(3,3);
pt.output(5,5);
pt.output();//result 3,3 使用this解决函数参数和成员变量的重名问题
/*
Point pt;
pt=new Point();
pt.output();//result 1,1 默认不带参数构造函数可以调用带参数的构造函数
*/
/*
Point pt;
pt=new Point();
Point pt2=new Point();
pt.x=6;
pt2.x=7;
System.out.println(pt.x);
System.out.println(pt2.x);//方法只有一份拷贝 而数据成员却有两份不同的拷贝
*/
/*
//静态函数 static void output() 的调用
Point.output();//直接通过类调用
pt=new Point();
pt.output();//对象调用静态方法实质还是通过类调用
*/
/*
//静态变量z的共享
Point pt1=new Point();
Point pt2=new Point();
pt1.z=6;
pt2.z=7;//pt1和pt2共享 z变量
System.out.println(pt1.z);// result 7
System.out.println(pt2.z);// result 7
*/
}
}
//***********************************
6.特殊变量this--类自身的引用
关于实例方法和实例数据成员的进一步说明
一个类所有的实例(对象)调用的成员方法在内存中只有一份拷贝,尽管在内存中可能有多个对象,而数据成员在类的每个对象所在内存中都存在着一份拷贝。this变量允许相同的实例方法为不同的对象工作。每当调用一个实例方法时,this变量将被设置成引用该实例方法的特定的类对象。方法的代码接着会与this所代表的对象的特定数据建立关联。
特殊变量super,提供了对父类的访问。
可以使用super访问父类被子类隐藏的变量或覆盖的方法。
每个子类构造方法的第一条语句,都是隐含地调用super(),
如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
Animal和Fish类测试程序如下://***********************************
- <SPAN style="FONT-SIZE: 18px">class Animal
- {
- int height,weight;
- /*
- //构造函数不能被继承
- Animal()
- {
- System.out.println("animal construct");//先调用基类构造函数
- }
- */
- Animal(int x, int y)
- {
- System.out.println("animal construct");//先调用基类构造函数
- }
- void Eat()
- {
- System.out.println("animal eat");
- }
- void Sleep()
- {
- System.out.println("animal sleep");
- }
- void Breath()
- {
- System.out.println("animal breath");
- }
- }
- //java 不允许多继承
- class Fish extends Animal
- {
- int height;
- Fish()
- {
- //super();//隐含调用不带参数构造函数
- super(30,30);//调用父类带参数的构造函数
- System.out.println("fish construct");//后调用派生类构造函数
- }
- void Breath()//函数的覆盖 发生在父类与子类之间 函数的重载在一个类内
- {
- //super.Breath(); //调用父类方法
- super.height=40;//调用父类成员变量
- System.out.println("fish bubble");
- }
- }
- class Integration
- {
- static void fn(Animal an)
- {
- an.Breath();
- }
- public static void main(String [] args)
- {
- //Animal an=new Animal();
- Fish fh=new Fish();
- //fh.height=30;
- //fh.weight=30;
- //an.Breath();
- //fh.Breath();
- //多态性
- //子类有则调用子类,无则调用父类
- /*
- Animal an;
- Fish fh=new Fish();
- an=fh;
- Integration.fn(an);//实际调用子类Breath方法
- */
- /*
- //类型判断
- Animal an=new Animal(30,40);
- Fish fh=new Fish();
- //Fish 是Animal Animal 不是Fish
- if(an instanceof Animal )
- {
- System.out.println("Animal instance");
- }
- if(fh instanceof Fish )
- {
- System.out.println("fish instance");
- }
- */
- /*
- Animal an=new Animal(30,40);
- Fish fh=new Fish();
- an=fh;
- if(an instanceof Fish )
- {
- System.out.println("an is Fish instance");//an虽然声明为Animal但赋值为Fish引用 an is Fish instance
- }
- else
- {
- System.out.println("an is Animal instance");
- }
- if(fh instanceof Animal )
- {
- System.out.println("fh is Animal instance");//Fish 是Animal fh is Animal instance
- }
- else
- {
- System.out.println("fh is fish instance");
- }
- */
- }
- }</SPAN>
class Animal
{
int height,weight;
/*
//构造函数不能被继承
Animal()
{
System.out.println("animal construct");//先调用基类构造函数
}
*/
Animal(int x, int y)
{
System.out.println("animal construct");//先调用基类构造函数
}
void Eat()
{
System.out.println("animal eat");
}
void Sleep()
{
System.out.println("animal sleep");
}
void Breath()
{
System.out.println("animal breath");
}
}
//java 不允许多继承
class Fish extends Animal
{
int height;
Fish()
{
//super();//隐含调用不带参数构造函数
super(30,30);//调用父类带参数的构造函数
System.out.println("fish construct");//后调用派生类构造函数
}
void Breath()//函数的覆盖 发生在父类与子类之间 函数的重载在一个类内
{
//super.Breath(); //调用父类方法
super.height=40;//调用父类成员变量
System.out.println("fish bubble");
}
}
class Integration
{
static void fn(Animal an)
{
an.Breath();
}
public static void main(String [] args)
{
//Animal an=new Animal();
Fish fh=new Fish();
//fh.height=30;
//fh.weight=30;
//an.Breath();
//fh.Breath();
//多态性
//子类有则调用子类,无则调用父类
/*
Animal an;
Fish fh=new Fish();
an=fh;
Integration.fn(an);//实际调用子类Breath方法
*/
/*
//类型判断
Animal an=new Animal(30,40);
Fish fh=new Fish();
//Fish 是Animal Animal 不是Fish
if(an instanceof Animal )
{
System.out.println("Animal instance");
}
if(fh instanceof Fish )
{
System.out.println("fish instance");
}
*/
/*
Animal an=new Animal(30,40);
Fish fh=new Fish();
an=fh;
if(an instanceof Fish )
{
System.out.println("an is Fish instance");//an虽然声明为Animal但赋值为Fish引用 an is Fish instance
}
else
{
System.out.println("an is Animal instance");
}
if(fh instanceof Animal )
{
System.out.println("fh is Animal instance");//Fish 是Animal fh is Animal instance
}
else
{
System.out.println("fh is fish instance");
}
*/
}
}
//**********************************