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());}}
- Java第二课 Java面向对象编程,面向对象编程和面向过程编程的区别,我们如何才能掌握面向对象的编程,类和对象的关系;讲解了Java中的构造、重载、this和super变量、静态变量、Java中的常
- PHP中的面向对象和面向过程的编程方法
- 面向过程和面向对象的编程
- 《面向过程和面向对象的编程》
- 面向过程和面向对象的编程
- 面向过程编程和面向对象编程的区别
- 面向过程编程和面向对象编程的区别
- java面向对象思想之方法的重载、this变量、静态变量、Java中的常量
- 面向对象和面向结构的编程特点和区别
- Java的面向对象编程
- JAVA的面向对象编程
- java面向对象的编程
- 面向过程和面向对象的编程-的区别
- 关于初学者如何理解面向对象和面向过程的区别以及java中的类和对象
- 面向对象编程和面向过程的区别
- 面向对象编程和面向过程编程的差异!
- Java笔记 第四章(2)Java面向对象编程基础 第二部分(类的成员变量和方法)
- 学习面向对象编程和面向接口编程的区别?
- TDD应用试例(根据领域驱动模型设计的培训内容)
- 画虚拟矩形框(适合静态的)
- RegCleanPro (微软认证-注册表清理软件)
- 关于消息格式化的方法
- 给tabbar添加背景
- Java第二课 Java面向对象编程,面向对象编程和面向过程编程的区别,我们如何才能掌握面向对象的编程,类和对象的关系;讲解了Java中的构造、重载、this和super变量、静态变量、Java中的常
- Android动画效果 translate、scale、alpha、rotate 切换Activity动画 控件位置调整
- B+数的插入实现
- 四极管 整理wince挂起和唤醒(suspend/wakeup)以及实现关机功能文章
- 毕设-02.16 搭建Eclipse开发环境,建立vlc源码的工程
- Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=
- 多种查看网页http header信息的方法
- C# 命名规则(匈牙利命名法)
- sizeof strlen() char * char []