ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:18.84KB ,
资源ID:10346655      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10346655.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(面向对象编程OOP.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

面向对象编程OOP.docx

1、面向对象编程OOP面向对象编程(OOP) 面向对象的三大特性 封装:隐藏实现细节 继承:一个类从另一个类中得到其授权的成员 多态:方法有多种形态 封装 类(抽象的概念) 普通类(实例类):能创建对象的类如:/实例类class Person 抽象类 以abstract所修饰的类 抽象类不能构建对象如:/抽象类abstract class User 示例1:public class ClassExam public static void main(String args) Person zhangsan=new Person();/引用实例类构建对象 Person jixufan=new Per

2、son(); System.out.println(zhangsan); System.out.println(jixufan); /User lisi=new User();/错误!asbstract not create instance /实例类class Person /抽象类abstract class User 对象(类的实例) 由属性和行为集合成的实例 由类构建的示例2:public class ObjectExam public static void main(String args) Student zs=new Student();/构建对象 zs.studNo=0001

3、; zs.studName=张三; zs.studAge=16; zs.Registying(); zs.studying(); Student lisi=new Student(); lisi.studNo=0002; lisi.studName=李四; lisi.studAge=20; lisi.Registying(); lisi.studying(); Student ww=new Student(); ww.studNo=0003; ww.studName=王老五; ww.Registying(); class Student /属性 public String studNo=000

4、0; public String studName=学生; public int studAge=18; public void studying() System.out.println(studName+正在学习); public void Registying() System.out.println(学号:+studNo +t姓名:+studName +t年龄:+studAge); 成员字段 类中的数据项(属性) 实质上就是类中的变量 普通的成员字段(实例属性)如:class Student /数据字段 String sNo=0001; String sName=姓名; int iAg

5、e=0; 示例3:调用实例成员字段public class MemberFieldExam public static void main(String args) MovieStar ldh=new MovieStar(); /实例成员字段用对象调用 ldh.level=No.1; ldh.bynl=一级棒; ldh.hf=true; String s=(ldh.hf)?已婚:未婚; System.out.println(ldh.level+ldh.bynl+s); class MovieStar /实例成员字段 public String level; public String bynl

6、; public boolean hf=false; 静态的成员字段(类的属性) 常用于公共类中的公共变量如:/静态属性 static String sShcool; 调用时使用类名如:Student.sShcool=清华大学; 成员方法 普通方法(实例方法)如:class Student public void studing() System.out.println(正在学习!); 示例4: 调用普通方法public class MethodExam public static void main(String args) /调用实例方法 Student objZS=new Student

7、(); objZS.studing(); 静态方法(类的方法) 常应用于公共类中的公共方法 静态方法中只能操作静态的数据如:class Student static String s1=变量; /静态方法 public static void sleeping() System.out.println(正在睡觉!); System.out.println(s1); F1(); static void F1() System.out.println(静态方法); 示例5: 调用静态方法public class MemberMethodExam public static void main(St

8、ring args) MStudent kx=new MStudent(); kx.studying(); MStudent.eating(); class MStudent static String studName=康熙; /实例方法 public void studying() System.out.println(studName+正在学习!); /静态方法 public static void eating() String s=雍正; /静态方法中只能操作静态的数据 System.out.println(studName+正在吃后悔药!); System.out.println(

9、s+正在吃后悔药!); MStudent yz=new MStudent(); yz.studying(); practice(); public static void practice() System.out.println(studName+正在实习!); 抽象方法(abstract修饰的方法) 定义在抽象类中 只有方法声明没有执行体如:abstract class User /定义抽象方法 public abstract boolean login(); 使用其的派生类对象调用示例6:public class MethodExam public static void main(St

10、ring args) /调用抽象方法 superUser objSU=new superUser(); boolean bFlag=objSU.login(); if(bFlag) System.out.println(登录成功); else System.out.println(登录失败); /User的派生类class superUser extends User public boolean login() System.out.println(超级管理员正在登录.); return true; 访问修饰符 public:公有(同一项目中的类都能被访问) protected:受保护(不同

11、包时,只能有继承关系的类中被访问,一般修饰类的成员) 默认:(只在同一包中的类被访问) private:私有(只能在同一个类中被访问,只能修饰类的成员)示例7:public class ModifierTest public static void main(String args) /public的类 GeralClassExam objGCE=new GeralClassExam(); /不同包中默认的类 /Student objZS=new Student();不充许被访问 /私有的类成员 objGCE.F1(); System.out.println(objGCE.s1); /受保护的

12、类成员 Animal objP=new Animal(); objP.speaking(); /objP.eating();/不能访问 构造方法 没有返回类型,与类同名,是类Public的成员 用来创建对象 默认的构造(隐式) 自定义构造(显式)示例8:public class ConsMethod public static void main(String args) BStudent zs=new BStudent(); class BStudent public BStudent()/显式 System.out.println(正在构造对象); 包(package) 包是用来管理类名的

13、。 打包 package 包名(小写) Java中常用的包 java.lang:语言包(不用导入) java.util:公用程序包 java.awt:抽象窗口工具包 java.sql:数据库组件包 java.io:输入输出流包 :网络编程包 java.math:数学公式包 javax.swing:轻量级组件包 接口(Interface) 类似公用的静态的抽象类 接口中一般定义方法(类似抽象方法)只有声明没有方法体示例8:定义接口/定义接口public interface IAct String status=关;/类似于静态常量 public boolean ON();/类似于抽象方法 pub

14、lic boolean OFF();/接口默认的修饰符是隐式的publicinterface IAct1 示例9:实现接口(implements)public class ImplementsInterface public static void main(String args) / TODO Auto-generated method stub TV objKJ=new TV(); objKJ.on(); ELight objEL=new ELight(); objEL.on(); /ISwitch.status=aba;/不能改变,因其为常量 System.out.println(IS

15、witch.status); /实现接口的类class TV implements ISwitch public boolean off() System.out.println(关电视机); return true; public boolean on() System.out.println(开电视机); return true; class ELight implements ISwitch public boolean off() System.out.println(关电灯); return true; public boolean on() System.out.println(开

16、电灯); return true; 继承(extends) 所类的根类:Object 基类与派生类示例10:/基类public class Animal/派生类class Person extends Animal /派生类class Chinese extends Person 成员继承性(私有的成员无法继承)示例11:/基类public class Animal public void speaking() System.out.println(yiyiyaya!); protected void eating() System.out.println(gegegege!); /派生类cl

17、ass Person extends Animal private void spleep() System.out.println(zzzzzzzz!); /派生类class Chinese extends Person public class InheritTest public static void main(String args) Chinese objP=new Chinese(); objP.speaking(); objP.eating(); 子类调用父类的成员与构造 this.成员名(本类的对象) super.成员名(父类的对象) 调用父类的构造: super(),且要放

18、在第一行。示例12:public class ClassExtendsExam public static void main(String args) /Chinese zs=new Chinese(张三,19); /Chinese ls=new Chinese(李四,20); /zs.name=张三; /System.out.println(zs.name); /zs.eating(); /zs.sleeping(); Cat objC=new Cat(猫咪1,3); Cat objC1=new Cat(猫咪2,4); /基类class Animal private String name

19、; private int age; public Animal(String sName,int iAge) name=sName; age=iAge; System.out.println(Animal的+name+t+age); /派生类class Cat extends Animal public Cat(String sN,int iA) super(sN,iA);/放在第一行 System.out.println(猫的构造); class Dog extends Animal public Dog() super(狗狗,4);/放在第一行 System.out.println(狗的

20、构造); 接口之间的继承示例13:public interface IAct1 extends IAct2 void F1(); void F2();interface IAct2 void F3(); void F4(); Java中不允许类之间多重继承,而使用多重接口实现。示例14:class B public void doing() System.out.println(正在做.); /一个接口继承一个类并实现多个接口class A extends B implements IAct2,IAct3 public void eating() / TODO Auto-generated m

21、ethod stub public void sleeping() / TODO Auto-generated method stub public void walking() / TODO Auto-generated method stub public void runing() / TODO Auto-generated method stub interface IAct1 public void walking(); public void sleeping();/接口的继承interface IAct2 extends IAct1 public void eating();in

22、terface IAct3 public void runing(); 多态(polymorphically) 方法重载 同一类中有同名的方法示例15:public class MethodFraughtExam public static void main(String args) Person zs=new Person(); zs.eating(苹果); zs.eating(苹果,3); zs.eating(); class Person /方法重载 public void eating() System.out.println(吃后悔药); public boolean eating

23、(String sFoot) System.out.println(吃+sFoot); return true; public void eating(String sFoot,int iHours) System.out.println(吃了+iHours+小时的+sFoot); 方法重写 子类中有父类中的同名方法示例16:public class MethodOverridExam public static void main(String args) B objB=new B(); objB.gongfu(); class A public void gongfu() System.out.println(打太极拳); class B extends A /重写了父类中的方法 public void gongfu() /super.gongfu(); System.out.println(打少林拳);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1