java程序设计--孙鑫java无难事Lesson2《位运算及面向对象程序设计》

java程序设计--孙鑫java无难事Lesson2《位运算及面向对象程序设计》


1.位运算

Java中有三个移位运算符
左移:<<
带符号右移:>>
无符号右移:>>>这部分内容可参见任何一本计算机操作系统或者计算机导论类书籍。
位运算测试程序如下:
//***********************************
  1. class  Test  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.           
  6.       int i=0xFFFFFFFF;//-1   
  7.       int c=i<<2;  
  8.       int d=i>>2;//有符号右移   
  9.       int e=i>>>2;//无符号右移   
  10.       System.out.println(i);//-1   
  11.       System.out.println(c);//-4   
  12.       System.out.println(Integer.toHexString(c));//有符号左移 fffffffc   
  13.       System.out.println(Integer.toHexString(d));//有符号右移 ffffffff   
  14.       System.out.println(Integer.toHexString(e));//无符号右移 3fffffff   
  15.     }  
  16. }  
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)概念

简单的说,面向对象编程(Object Oriented Programming,简称OOP)描述的是对象之间的相互作用。面向对象编程与面向过程编程的区别:过程化程序设计先确定算法,再确定数据结构;面向对象编程先确定数据结构,再确定运算。面向过程编程的程序员,习惯于建立数据结构存放数据并定义方法(函数)来操作数据;面向对象编程的程序员则构造一个 对象模型,将数据与方法组织在一起。

(2)面向对象解决问题-计算矩形周长和面积
Rectangle类测试程序如下:
//***********************************

  1. <SPAN style="FONT-SIZE: 18px">class  Rectangle  
  2. {  
  3.     int w,l;  
  4.     int perimeter()  
  5.     {  
  6.       return 2*(l+w);  
  7.     }  
  8.     int area()  
  9.     {  
  10.       return l*w;  
  11.     }  
  12. public static void main(String[] args)   
  13.     {     
  14.         Rectangle rect1=new Rectangle();  
  15.         Rectangle rect2=new Rectangle();  
  16.         rect1.l=10;  
  17.         rect1.w=5;  
  18.         rect2.l=4;  
  19.         rect2.w=6;  
  20.         System.out.println("the perimeter of rect1 is "+rect1.perimeter());//the perimeter of rect1 is 30   
  21.         System.out.println("the area of rect2 is "+rect2.area());//the area of rect2 is 24   
  22.     }  
  23. }</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类测试程序如下:
//***********************************
  1. <SPAN style="FONT-SIZE: 18px">class  Point  
  2. {     
  3.     int x,y;//对象变量   
  4.     static int z;//类变量属于类本身,不属于某个对象   
  5.     //常量必须赋初值  3.1415926 定义为非静态时可以放在构造函数中     
  6.     //定义为静态的 共享该数据 避免每个对象的拷贝  此时必须在定义时初始化   
  7.     static final double PI=3.1415926;//静态常量必须再此初始化   
  8.     Point()  
  9.     {    
  10.        //PI=3.1415926;//ok  非静态常量可以这里初始化   
  11.        this(1,1);//调用带参数的构造函数  必须为第一条语句   
  12.     }  
  13.     Point(int a,int b)  
  14.     {   
  15.       //PI=3.1415926;//ok  非静态常量可以这里初始化   
  16.       x=a;  
  17.       y=b;  
  18.     }  
  19.     void output(int x,int y)  
  20.     {  
  21.       this.x=x;  
  22.       this.y=y;//内部成员变量被隐藏 这里只是形参可见 因此采用this   
  23.     }  
  24.     /* 
  25.     //实例方法 
  26.     void output() 
  27.     { 
  28.       System.out.println(x); 
  29.       System.out.println(y); 
  30.     } 
  31.     */  
  32.     /* 
  33.     //静态方法 
  34.     static void output() 
  35.     { 
  36.       System.out.println("output called!"); 
  37.       System.out.println(x);//error  静态方法引用非静态成员变量 
  38.       System.out.println(y);//error 
  39.     }*/  
  40.     static void output()  
  41.     {  
  42.       System.out.println("output called!");  
  43.       System.out.println(z);  
  44.     }  
  45.     public static void main(String[] args)   
  46.     {   /* 
  47.          Point pt; 
  48.         pt=new Point(); 
  49.         pt.x=10; 
  50.         pt.y=10; 
  51.         pt.output();//输出类的成员变量信息 
  52.         */  
  53.         /* 
  54.         Point pt; 
  55.         pt=new Point(3,3); 
  56.         pt.output(5,5); 
  57.         pt.output();//result 3,3  使用this解决函数参数和成员变量的重名问题 
  58.         /* 
  59.         Point pt; 
  60.         pt=new Point(); 
  61.         pt.output();//result 1,1  默认不带参数构造函数可以调用带参数的构造函数 
  62.         */  
  63.         /* 
  64.         Point pt; 
  65.         pt=new Point(); 
  66.         Point pt2=new Point(); 
  67.         pt.x=6; 
  68.         pt2.x=7; 
  69.         System.out.println(pt.x); 
  70.         System.out.println(pt2.x);//方法只有一份拷贝 而数据成员却有两份不同的拷贝 
  71.         */  
  72.         /* 
  73.         //静态函数  static void output()  的调用 
  74.         Point.output();//直接通过类调用 
  75.         pt=new Point(); 
  76.         pt.output();//对象调用静态方法实质还是通过类调用 
  77.         */  
  78.         /* 
  79.         //静态变量z的共享 
  80.         Point pt1=new Point(); 
  81.         Point pt2=new Point(); 
  82.         pt1.z=6; 
  83.         pt2.z=7;//pt1和pt2共享 z变量 
  84.         System.out.println(pt1.z);// result 7 
  85.         System.out.println(pt2.z);// result 7 
  86.         */  
  87.     }  
  88. }</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所代表的对象的特定数据建立关联。

7.特殊变量super-实现父类成员变量和函数的引用
特殊变量super,提供了对父类的访问。
可以使用super访问父类被子类隐藏的变量或覆盖的方法。

每个子类构造方法的第一条语句,都是隐含地调用super(),

如果父类没有这种形式的构造函数,那么在编译的时候就会报错。

Animal和Fish类测试程序如下:
//***********************************
  1. <SPAN style="FONT-SIZE: 18px">class Animal  
  2. {     
  3.     int height,weight;  
  4.     /* 
  5.     //构造函数不能被继承 
  6.     Animal() 
  7.     { 
  8.         System.out.println("animal construct");//先调用基类构造函数 
  9.     } 
  10.     */  
  11.     Animal(int x, int y)  
  12.     {  
  13.         System.out.println("animal construct");//先调用基类构造函数   
  14.     }  
  15.     void Eat()  
  16.     {  
  17.         System.out.println("animal eat");  
  18.     }  
  19.     void Sleep()  
  20.     {  
  21.         System.out.println("animal sleep");  
  22.     }  
  23.     void Breath()  
  24.     {  
  25.         System.out.println("animal breath");  
  26.     }  
  27. }  
  28. //java 不允许多继承   
  29. class Fish extends Animal  
  30. {     
  31.     int height;  
  32.     Fish()  
  33.     {     
  34.         //super();//隐含调用不带参数构造函数   
  35.         super(30,30);//调用父类带参数的构造函数   
  36.         System.out.println("fish construct");//后调用派生类构造函数   
  37.     }  
  38.       
  39.     void Breath()//函数的覆盖 发生在父类与子类之间  函数的重载在一个类内   
  40.     {     
  41.         //super.Breath(); //调用父类方法   
  42.         super.height=40;//调用父类成员变量   
  43.         System.out.println("fish bubble");  
  44.     }  
  45. }  
  46. class Integration  
  47. {     
  48.     static void fn(Animal an)  
  49.     {  
  50.         an.Breath();  
  51.     }  
  52.     public static void main(String [] args)  
  53.     {  
  54.         //Animal an=new Animal();   
  55.         Fish   fh=new Fish();  
  56.         //fh.height=30;   
  57.         //fh.weight=30;   
  58.         //an.Breath();   
  59.         //fh.Breath();   
  60.         //多态性   
  61.         //子类有则调用子类,无则调用父类   
  62.         /* 
  63.         Animal an; 
  64.         Fish fh=new Fish(); 
  65.         an=fh; 
  66.         Integration.fn(an);//实际调用子类Breath方法  
  67.         */  
  68.         /* 
  69.         //类型判断 
  70.         Animal an=new Animal(30,40); 
  71.         Fish fh=new Fish(); 
  72.         //Fish 是Animal  Animal 不是Fish 
  73.         if(an instanceof Animal ) 
  74.         { 
  75.             System.out.println("Animal instance"); 
  76.         } 
  77.         if(fh instanceof Fish ) 
  78.         { 
  79.             System.out.println("fish instance"); 
  80.         } 
  81.         */  
  82.         /* 
  83.         Animal an=new Animal(30,40); 
  84.         Fish fh=new Fish(); 
  85.         an=fh; 
  86.         if(an instanceof Fish ) 
  87.         { 
  88.             System.out.println("an is Fish instance");//an虽然声明为Animal但赋值为Fish引用 an is Fish instance 
  89.         } 
  90.         else 
  91.         { 
  92.             System.out.println("an is Animal instance"); 
  93.         } 
  94.         if(fh instanceof Animal ) 
  95.         { 
  96.             System.out.println("fh is Animal instance");//Fish 是Animal   fh is Animal instance 
  97.         } 
  98.         else 
  99.         { 
  100.             System.out.println("fh is fish instance"); 
  101.         } 
  102.         */  
  103.     }  
  104. }</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");
		}
		*/
	}
}
//**********************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值