首页 文章 精选 留言 我的

精选列表

搜索[对象模型],共10000篇文章
优秀的个人博客,低调大师

Java 面向对象 之 方法中内部类

http://www.verejava.com/?id=16992857876159 /** 知识点: 内部类 1. 内部类的定义 2. 内部类的优缺点 3. 外部类怎么实例化其他类的内部类 4. 方法中定义内部类 如果在方法中定义内部类,方法中的内部类要访问变量, 需要加final 关键字 5. static 声明内部类 题目:母亲怀了孕, 母亲的营养决定孩子的健康成长 思路: 1. 抽象出类 : 母亲(Mother), 孩子(Baby) 2. 找出类的关系: 孩子在母亲里面 Baby in Mother 3. 抽象出方法: 母亲吃东西 (eat) */ public class InnerClass2 { public static void main(String[] args) { //实例化母亲 Mother mother = new Mother("lucy"); //母亲吃苹果 mother.eat("苹果"); } } class Mother { private String name; private String food;//母亲吃的食物 public Mother(String name) { this.name = name; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } //母亲吃东西 public void eat(final String food) { this.food = food; System.out.println(this.name + " 吃了 " + this.food); class Baby { //孩子从母亲吃的东西中吸收营养 public void eat() { System.out.println("孩子从母亲吃的 " + food + " 中吸收营养"); } } //母亲吃东西的同时 孩子也吃东西 new Baby().eat(); } } http://www.verejava.com/?id=16992857876159

优秀的个人博客,低调大师

Java 面向对象 之 人吃水果

http://www.verejava.com/?id=16992850564257 /** 题目: 人可以(孩子,学生,老师) 吃 水果(苹果,葡萄,芒果,菠萝) 思路: 1. 抽象出类 : 人(Person){孩子(Baby),学生(Student)}, 水果(Fruit){苹果(Apple),葡萄(Grape),芒果(Mango)} 2. 找出类的关系: 水果 -> 人 (孩子,学生) 是 人的分类 也是人只是声明可以吃水果, 但是吃水果的具体执行者 是 (孩子,学生) (苹果,葡萄,芒果) 是 水果的分类 3. 找出属性: 孩子,学生(姓名,水果引用) 4. 找出方法: 孩子,学生都吃(eat)水果 子类->父类的转换 子类->父类后 父类引用调用子类复写的方法 作用 :解耦 也就是降低类与类之间的耦合度 提供可扩展性 */ public class Polymorphism5 { //打印人吃水果的信息 public void output(Person p) { //实例化一个苹果 和 一个葡萄 Apple apple = new Apple("红富士"); Grape grape = new Grape("黑葡萄"); Mango mango = new Mango("大芒果"); PineApple pineApple = new PineApple("菠萝"); p.eat(apple); System.out.println(p.getName() + " 吃了 " + p.getFruit().getName()); p.eat(grape); System.out.println(p.getName() + " 吃了 " + p.getFruit().getName()); p.eat(mango); System.out.println(p.getName() + " 吃了 " + p.getFruit().getName()); p.eat(pineApple); System.out.println(p.getName() + " 吃了 " + p.getFruit().getName()); } public static void main(String[] args) { Polymorphism5 test = new Polymorphism5(); //李明吃水果 test.output(new Baby("李明")); //张涛吃水果 test.output(new Student("张涛")); //郭老师吃水果 test.output(new Teacher("郭老师")); } } abstract class Person { protected Fruit fruit; protected String name; public Person(String name) { this.name = name; } public String getName() { return this.name; } public Fruit getFruit() { return this.fruit; } //声明人是可以吃水果的 public abstract void eat(Fruit fruit); } class Teacher extends Person { public Teacher(String name) { super(name); } //吃水果 public void eat(Fruit fruit) { this.fruit = fruit; } } class Baby extends Person { public Baby(String name) { super(name); } //吃水果 public void eat(Fruit fruit) { this.fruit = fruit; } } class Student extends Person { public Student(String name) { super(name); } //吃水果 public void eat(Fruit fruit) { this.fruit = fruit; } } class Fruit { private String name; public Fruit(String name) { this.name = name; } public String getName() { return this.name; } } class PineApple extends Fruit { public PineApple(String name) { super(name); } } class Apple extends Fruit { public Apple(String name) { super(name); } } class Grape extends Fruit { public Grape(String name) { super(name); } } class Mango extends Fruit { public Mango(String name) { super(name); } } http://www.verejava.com/?id=16992850564257

优秀的个人博客,低调大师

Java 面向对象 之 孩子 学生 吃 水果

http://www.verejava.com/?id=16992848300456 /** 题目: (孩子,学生) 吃 水果(苹果,葡萄,芒果,菠萝) 思路: 1. 抽象出类 : {孩子(Baby),学生(Student)}, 水果(Fruit){苹果(Apple),葡萄(Grape),芒果(Mango)} 2. 找出类的关系: 水果 -> (孩子,学生) (苹果,葡萄,芒果) 是 水果的分类 3. 找出属性: 孩子,学生(姓名,水果引用) 4. 找出方法: 孩子,学生都吃(eat)水果 子类->父类的转换 作用 :解耦 也就是降低类与类之间的耦合度 提供可扩展性 */ public class Polymorphism4 { public static void main(String[] args) { //实例化一个Baby Baby baby = new Baby("李明"); //实例化一个学生 Student stu = new Student("张涛"); //实例化一个苹果 和 一个葡萄 Apple apple = new Apple("红富士"); Grape grape = new Grape("黑葡萄"); Mango mango = new Mango("大芒果"); PineApple pineApple = new PineApple("菠萝"); //李明吃水果 baby.eat(apple); System.out.println(baby.getName() + " 吃了 " + baby.getFruit().getName()); baby.eat(grape); System.out.println(baby.getName() + " 吃了 " + baby.getFruit().getName()); baby.eat(mango); System.out.println(baby.getName() + " 吃了 " + baby.getFruit().getName()); baby.eat(pineApple); System.out.println(baby.getName() + " 吃了 " + baby.getFruit().getName()); //张涛吃水果 stu.eat(apple); System.out.println(stu.getName() + " 吃了 " + stu.getFruit().getName()); stu.eat(grape); System.out.println(stu.getName() + " 吃了 " + stu.getFruit().getName()); stu.eat(mango); System.out.println(stu.getName() + " 吃了 " + stu.getFruit().getName()); stu.eat(pineApple); System.out.println(stu.getName() + " 吃了 " + stu.getFruit().getName()); } } class Baby { private Fruit fruit; private String name; public Baby(String name) { this.name = name; } public String getName() { return this.name; } public Fruit getFruit() { return this.fruit; } //吃水果 public void eat(Fruit fruit) { this.fruit = fruit; } } class Student { private Fruit fruit; private String name; public Student(String name) { this.name = name; } public String getName() { return this.name; } public Fruit getFruit() { return this.fruit; } //吃水果 public void eat(Fruit fruit) { this.fruit = fruit; } } class Fruit { private String name; public Fruit(String name) { this.name = name; } public String getName() { return this.name; } } class PineApple extends Fruit { public PineApple(String name) { super(name); } } class Apple extends Fruit { public Apple(String name) { super(name); } } class Grape extends Fruit { public Grape(String name) { super(name); } } class Mango extends Fruit { public Mango(String name) { super(name); } } http://www.verejava.com/?id=16992848300456

优秀的个人博客,低调大师

Java 面向对象 之 final 关键字

http://www.verejava.com/?id=16992827836151 /** 知识点: final 关键字 1. 什么是final : final 就是最终的意思 2. final 关键字修饰的变量是常量 不能修改 3. final 关键字修饰的方法,子类不能复写 4. final 关键字修饰的类,不能被继承 */ public class FinalKeyword { public static void main(String[] args) { final double PI = 3.14; System.out.println(PI); //实例化父亲 Father f = new Father(); f.work(); //实例化儿子 Son s = new Son(); s.study(); } } //父亲 class Father { public final void work() { System.out.println("干农活"); } } //儿子 class Son extends Father { public void study() { System.out.println("好好学习,天天向上"); } } //女儿 final class Daughter extends Father { } //孙子 class GranSon extends Son { } http://www.verejava.com/?id=16992827836151

优秀的个人博客,低调大师

Java 面向对象 之 super 关键字

http://www.verejava.com/?id=17159596599630 /** this: 代表当前类的引用 1. 当局部变量和成员变量同名时, 成员变量要加 this 限定 2. 实例化时 可以用 this 调用当前类的构造方法, 必须写在第一行 3. 可以用 this 调用当前类的 普通方法 super : 代表当前父类的引用 1. 实例化子类时, 可以用 super 调用父类的 非私有方法 2. 实例化子类时. 可以用 super 调用父类的 构造方法 , 必须写在第一行 3. 在子类的方法中 , 可以用 supe 调用父类的 非私有方法. */ public class Test1 { public static void main(String[] args) { // 实例化 农夫 Father father = new Father(); father.setName("农夫"); father.setAge(90); System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth()); Father father = new Father("农夫", 90); System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth()); Son son = new Son("农夫", 90); System.out.println("儿子知道父亲的 : " + son.getAge() + " 岁 " + son.getName() + " 有 " + son.getWealth()); son.work(); } } //父类 class Father { private String name; private int age; private String wealth; public Father() { wealth = "100两黄金"; } public Father(String name, int age) { this(); this.name = name; //this.age=age; this.setAge(90); } public void work() { System.out.println("耕地"); } public String getWealth() { return wealth; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } } //子类 class Son extends Father { public Son(String name, int age) { super(name, age); //super.setName(name); //super.setAge(age); } public void work() { super.work(); System.out.println("儿子 寻找黄金宝藏"); System.out.println("只有通过自己的勤奋劳动, 才能得到果实 是最大的宝藏"); } } http://www.verejava.com/?id=17159596599630

优秀的个人博客,低调大师

Java 面向对象 之 关键字instanceof

http://www.verejava.com/?id=16992811364048 /** 知识点: 关键字 instanceof 题目:输出参加Oracle大会的人分类信息 思路: 1. 抽象出类 : 会议厅(Hall), 人种(Person){程序员(Programer),管理者(Manager)} 2. 找出类关系: 2.1 {程序员(Programer),管理者(Manager)} 是 人种(Person) 分类 2.2 人种(Person)->会议厅(Hall) 3. 找出属性: 3.1 会议厅(Hall)(会议名称,容纳人数) 3.2 人种(Person)(姓名) 4. 找出方法: 4.1 要参加会议 会议厅(Hall) 登记参加会议的每个人信息 register(Person p) 4.2 输出参加大会的人信息 output() */ public class TestInstanceof { public static void main(String[] args) { //Oracle说我要在Moscore center 举办一年一度的大会, 需要一个 //容纳1000人的会厅 //开一个能容量1000人的会厅 Hall h = new Hall(1000); //开始登记注册 h.register(new Programer("Joseph")); h.register(new Programer("james")); h.register(new Manager("Page")); h.register(new Manager("Joe")); //打印输出 h.output(); } } class Hall { private String name;//会议名称 private Person[] persons;//登记册 public Hall(int maxSize) { persons = new Person[maxSize]; } //登记每个参加会议的人信息 //思路: // 1. 循环检测如果没有超出容量,将该人添加到 登记册 // 如果超出容量,提示已经没有注册名额,注册完毕 public void register(Person newPerson) { int flag = 0;//检测是否注册完毕 for (int i = 0; i < persons.length; i++) { if (persons[i] == null) { persons[i] = newPerson; break; } flag++; } if (flag == persons.length) { System.out.println("已经没有注册名额,注册完毕"); } } //输出参加大会的人分类信息 // 思路: // 1. 循环打印出每个已经参加登记人的信息 // 2. 但是在循环打印的时候要判断 到底打印的是 程序员 还是 经理 public void output() { for (int i = 0; i < persons.length; i++) { //打印已经登记的人信息 if (persons[i] != null) { if (persons[i] instanceof Programer) { System.out.println(persons[i].getName() + " 属于程序员"); } if (persons[i] instanceof Manager) { System.out.println(persons[i].getName() + " 属于经理以上级别"); } } } } } abstract class Person { protected String name;//姓名 public Person(String name) { this.name = name; } public String getName() { return this.name; } } class Programer extends Person { public Programer(String name) { super(name); } } class Manager extends Person { public Manager(String name) { super(name); } } http://www.verejava.com/?id=16992811364048

优秀的个人博客,低调大师

Java 面向对象 之 抽象类 abstract

http://www.verejava.com/?id=16992804621647 /** 知识点: 抽象类 abstract 题目: 爸爸叫我买一斤水果和一条鱼 思路: 1. 抽象出类:爸爸(Father), 儿子(Son), 水果(Fruit),鱼(Fish) 2. 找出类关系: 儿子继承爸爸 水果买回来属于->Father->Son 3. 找出方法: 买(buy) 是爸爸叫儿子买水果,爸爸知识发出了要求声明并没用实现 而真正去实现这个是儿子 */ /** 注意: 1. 加了关键字 abstract 的抽象方法不能有方法体实现,只能有方法的声明 2. 当一个类中存在以 abstract 声明的抽象方法时, 该类必须加上abstract 关键 字表明该类是抽象类 3. 子类如果继承一个抽象类, 子类必须实现父类所有的抽象方法 4. 抽象类中也可以声明实现方法,也就是抽象类中可以定义抽象方法,也可以定义 带方法体的实现方法,而子类不需要复写抽象父类的具体已经实现的方法 5. 抽象类不能直接实例化,必须有其实现的子类间接来实例化 */ public class TestAbstract { public static void main(String[] args) { //实例化父亲 //Father f=new Father(); //实例化儿子 Son s = new Son("李明"); //地摊上有各种水果和鱼 Fruit apple = new Fruit("苹果"); Fruit grape = new Fruit("葡萄"); Fish caoyu = new Fish("草鱼"); Fish lianyu = new Fish("鲢鱼"); //买水果和鱼 s.buy(apple); s.buy(lianyu); //回家以后高兴的告诉爸爸 System.out.println("爸爸,爸爸 我买了一斤:" + s.getFruit().getName() + " 和一条 : " + s.getFish().getName()); } } abstract class Father { protected Fruit fruit; protected Fish fish; public Fruit getFruit() { return this.fruit; } public Fish getFish() { return this.fish; } //要求声明要买水果 public abstract void buy(Fruit fruit); //要求声明要买条鱼 public abstract void buy(Fish fish); } class Son extends Father { private String name; public Son(String name) { super(); this.name = name; } //儿子实现父亲买水果的要求 public void buy(Fruit fruit) { this.fruit = fruit; } //儿子实现父亲买条鱼的要求 public void buy(Fish fish) { this.fish = fish; } public String getName() { return this.name; } } class Fruit { private String name; public Fruit(String name) { this.name = name; } public String getName() { return this.name; } } class Fish { private String name; public Fish(String name) { this.name = name; } public String getName() { return this.name; } } http://www.verejava.com/?id=16992804621647

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册