Java第二课 Java面向对象编程,面向对象编程和面向过程编程的区别,我们如何才能掌握面向对象的编程,类和对象的关系;讲解了Java中的构造、重载、this和super变量、静态变量、Java中的常

来源:互联网 发布:淘宝买家好评率查询 编辑:程序博客网 时间:2024/06/03 16:23

位运算


class Test{public static void main(String[] args){int i=0xffffffff;//两个f表示一个字节所表示的二进制位   -1/*int c=i<<2;//左移int c=i>>2;//有符号右移int c=i>>>2;//无符号右移*///将一个整数110从右端开始的4~7位变为0int c=110&7;System.out.println(i);System.out.println(c);System.out.println(Integer.toBinaryString(c));System.out.println(Integer.toHexString(c));}}

面向对象的程序设计


/*用final声明的常量 *要么在声明时进行初始化 *要么在类的构造函数中进行初始化 **/class Point{int x;int y;static int z;static final double PI=3.1415926;//数据成员在所有对象的内存中都有一份拷贝Point(){    this(5,5);//调用Point带参数的构造函数//此举可简化构造函数的调用//PI=3.1415926;}Point(int a,int b){x=a;y=b;//PI=3.1415926;}//调用静态方法时不需要产生类的对象就可以直接调用static void output(){System.out.println("Static Method Output() Called !");//System.out.println("x="+x);//System.out.println("y="+y);}//静态方法:类方法void output(int x,int y){//区分成员变量和局部变量this.x=x;this.y=y;z=5;}//非静态方法:实例方法//成员方法在同一类的对象的内存中只有一份拷贝public static void main(String[] args){Point pt;/*pt=new Point();System.out.println(Point.z);pt.output(1,2); //非静态方法可以调用静态成员变量System.out.println(Point.z);*/}}



/*在调用子类的构造函数之前会先调用父类的构造函数 *使用super可以访问父类中被覆盖的成员方法和数据成员 *构造方法不能被继承 * *多态性通过覆盖父类的引用来实现 *在运行时根据传递对象的引用来调用相应的方法 *在将子类的引用传递给父类时 *如果子类有该方法就调用子类的方法 *子类没有就调用父类的方法 * *instanceof 判断一个类的对象是否是一个类的实例 *子类的实例也是父类的实例 **/class Animal{int height;int weight;Animal(){//System.out.println("Animal Construct !");}void eat(){System.out.println("Animal Eat !");}void sleep(){System.out.println("Animal Sleep !");}void breathe(){System.out.println("Animal Breathe !");}}class Fish extends Animal{Fish(){//super();显式调用super方法调用父类中不带参数的构造方法//super(1,1 );//不能写成Animal(1,1);//System.out.println("Fish Construct !");}//方法的覆盖/*void breathe(){//super.breathe();//访问父类中被覆盖的成员方法System.out.println("Fish Bubble !");}*/}class Integration{static void fn(Animal an){an.breathe();}public static void main(String[] args){//Animal an=new Animal();Fish fh=new Fish();//an.breathe();//an.height=175;//fh.breathe();Animal an;an=fh;//用instanceof判断会显示an是Fish的一个对象//Integration.fn(an);    //  Integration.fn(fh);//Animal an=new Animal();//Fish fh=new Fish();/*if(an instanceof Animal){System.out.println("an is Animal's instance .");}if(fh instanceof Fish){System.out.println("fh is Fish's instance .");}*/if(an instanceof Fish){System.out.println("an is Fish's instance .");}else{System.out.println("an is not Fish's instance .");}if(fh instanceof Animal){System.out.println("fh is Animal's instance .");}else{System.out.println("fh is not Animal's instance .");}}}


小练习:求矩形的面积和周长


class Rectangle{int height;int length;Rectangle(){}Rectangle(int height,int length){this.height=height;    this.length=length;}int perimeter(){return 2*(height+length);}int area(){return height*length;}public static void main(String[] args){Rectangle rec1=new Rectangle();//默认构造方法System.out.println("perimeter = "+rec1.perimeter());System.out.println("area = "+rec1.area());Rectangle rec2=new Rectangle(3,5);//带参数构造方法System.out.println("perimeter = "+rec2.perimeter());System.out.println("area = "+rec2.area());}}