大数据学习2019.03.15
Java EE进阶——面向对象程序设计01
对象变量是对象的管理者;
成员变量
- 类定义了对象中所有的变量,这些变量称作成员变量;
- 每个对象有自己的变量,和同一个类的其他对象是分开的。
函数与成员变量
在函数中可以直接写成员变量的名字来访问成员变量。
函数时通过对象来调用的。
this是成员函数的一个特殊的固有的本地变量,它表达了调用这个函数的那个对象。
调用函数
通过 .运算符调用某个对象的函数。
在成员函数内部调用自己(this)的其他函数。
本地变量:定义在函数内部的变量是本地变量。
本地变量的生存期和作用域都是在函数内部。
成员变量的生存期是对象的生存期,作用域是类内部的成员函数。
成员变量定义初始化
- 成员变量在定义的地方就可以给出初始值;
- 没有初始化的成员变量会自动获得0值,与本地变量不同,本地变量初始化时未给出初始值就使用,编译时就会报错;
- 对象变量的0值表示没有管理任何对象,也可以主动给null值;
- 定义初始化可以调用函数,甚至可以使用已经定义的成员变量。
构造函数
如果有一个成员函数的名字和类的名字完全相同,则在创建这个类的每一个对象的时候会自动调用这个函数-->构造函数。
这个函数不能有返回类型。
package test02; public class VendingMachine { // int price = 80; int balance = f(); int total; VendingMachine() { //构造函数 total = 0; } VendingMachine(int price){ //重载 this(); //调用无参构造函数 this.price = price; } int f() { return 10; } void showPrompt() { System.out.println("Welcome!"); } void insertMoney(int amount) { balance += amount; showBalance(); //this.showBalance(); } void showBalance() { System.out.println(balance); } void getFood() { if(balance >= price) { System.out.println("Here you are!"); balance -= price; total += price; } } public static void main(String[] args) { VendingMachine vm = new VendingMachine(); vm.showPrompt(); vm.showBalance(); vm.insertMoney(100); vm.getFood(); vm.showBalance(); VendingMachine vm1 = new VendingMachine(100); vm1.showPrompt(); vm1.showBalance(); vm1.insertMoney(200); //vm1.getFood(); vm1.showBalance(); } }
对象的识别
对象的交互
package clock; public class Display { private int value = 0; private int limit = 0; public Display(int limit) { this.limit = limit; } public void increase() { value++; if(value == limit) { value = 0; } } public int getValue() { return value; } public static void main(String[] args) { Display d = new Display(24); for(;;) { d.increase(); System.out.println(d.getValue()); } } }
package clock; public class Clock { private Display hour = new Display(24); private Display minute = new Display(60); public void start() { while(true) { minute.increase(); if(minute.getValue() == 0) { hour.increase(); } System.out.printf("%02d:%02d\n",hour.getValue(),minute.getValue()); } } public static void main(String[] args) { Clock clock = new Clock(); clock.start(); } }
封闭的访问属性
对象 = 属性 + 服务
数据:属性或状态
操作:函数
封装:把数据和对数据的操作放在一起;
private关键字只能用于成员变量和成员函数;
private
- 只有这个类内部可以访问;
- 类内部指类的成员函数和定义初始化;
- 这个限制是对类的而不是对对象的。
- 同一个类的不同对象可以相互访问私有成员变量。
public
- 任何人都可以访问;
- 任何人指的是任何类的函数或定义初始化中都可以使用;
- 使用指的是调用,访问或定义变量;
无public或private关键字修饰符,则默认为“friendly”
则位于同一个包的类可以访问,
包的概念
类变量
static关键字修饰类变量。
static成员变量是类拥有的,实例对象不拥有,但是所有该类的实例对象共享的,可以通过该类以及该类的对象访问,调用该类变量
类函数
static关键字修饰类函数。
static修饰的成员函数属于类,是所有该类的实例对象共享的。
static成员函数只能访问,调用static成员变量;
static变量,static函数可以通过类名调用,也可以通过类实例调用。
类变量只会在类的初次装载的时候初始化,新建对象时不会再次初始化。
记事本的例子
1.接口设计
- add(String note);
- getSize();
- getNote(int index);
- removeNote(int index);
- list();
人机交互与业务逻辑分离
泛型容器类型ArrayList
ArrayList<String> notes = new ArrayList<String>();
容器有两个类型:
- 容器的类型;
- 元素的类型。
ArrayList的操作
对象数组
对象数组中的每个元素都是对象的管理者而非对象本身。
for-each循环
package notebook; import java.util.ArrayList; public class NoteBook { private ArrayList<String> notes = new ArrayList<String>(); public String[] list() { String[] a = new String[notes.size()]; // for(int i = 0;i<notes.size();i++) { // a[i] = notes.get(i); // } notes.toArray(a); return a; } public void add(String s,int location) { notes.add(location, s); } public void removeNote(int index) { notes.remove(index); } public String getNote(int index) { return notes.get(index); } public int getSize() { return notes.size(); } public void add(String s) { notes.add(s); } public static void main(String[] s) { NoteBook nb = new NoteBook(); nb.add("first"); nb.add("second"); System.out.println(nb.getSize()); } }
集合容器Set,HashSet
package test02; import java.util.HashSet; public class HashSetTest01 { public static void main(String[] args) { HashSet<String> s = new HashSet<String>(); s.add("first"); s.add("Second"); s.add("first"); s.add("third"); for (String str : s) { System.out.println(str); } } }
集合中没有重复的元素,唯一性,无序性
ArrayList,HashSet可以直接输出
toString函数是任何一个Java类输出时默认调用的函数,重写toString方法可以改变类的输出方式。
Hash表
HashMap表中键值唯一;
package coins; import java.util.HashMap; import java.util.Scanner; public class Coin { private HashMap<Integer,String> coinNames = new HashMap<Integer,String>(); public Coin(){ coinNames.put(1, "penny"); coinNames.put(10, "dime"); coinNames.put(25, "quarter"); coinNames.put(50, "half-dollar"); } public String getName(int amount) { if(coinNames.containsKey(amount)) { return coinNames.get(amount); }else { return "NOT FOUND"; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int amount = in.nextInt(); Coin coin = new Coin(); String name = coin.getName(amount); System.out.println(name); in.close(); } }
浙公网安备 33010602011771号