The content of this article comes from "Object-Oriented Programming - Java language" written by Weng Kai, Zhejiang University, mooc.com. After learning, I sorted out this article, which is purely used for learning records.
Article directory
1. Object initialization
Constructor
- If the name of a member function is exactly the same as that of a class, the function will be called automatically when each object of the class is created;
- This function cannot have a return type;
heavy load
- A class can have multiple constructors, as long as their parameter tables are different;
- When creating objects, different parameter values will be given, and different constructors will be called automatically;
- Other constructors can be called through this();
- The functions with the same name but different parameter tables of a class form the overload relation;
package mooc_java.class_object; public class VendingMachine { int price = 10; int balance; int total; // Constructor VendingMachine() { total = 1; } // heavy load VendingMachine(int price) { this.price = price; } void showPrompt() { System.out.println("welcome to VendingMachine"); } void setPrice(int price) { this.price = price; } void insertMoney(int amount) { balance += amount; } void showBalance() { System.out.println(balance); } void getFood() { if (balance >= price) { System.out.println("Here you are"); balance = balance - price; total = total + price; showBalance(); } } public static void main(String[] args) { // create object VendingMachine vm = new VendingMachine(); // The object variable is the manager of the object vm.showPrompt(); vm.showBalance(); vm.setPrice(20); vm.insertMoney(100); vm.getFood(); VendingMachine vm2 = new VendingMachine(100); } }
Refer to: class_object
2. Object interaction
Object = property + Service
- Data: properties or states
- Actions: Functions
Put the data and the operation on the data together -- > package.
private: only this class can access
- Class internal refers to initialization of class member functions and definitions;
- This restriction is for classes, not objects;
public: anyone can access
- Anyone refers to the function or definition initialization of any class that can be used;
- Use refers to calling, accessing or defining variables;
Refer to: clock
3. container class
eg: ArrayList<String> strings = new ArrayList<String>();, HashSet<String> stringHashSet = new HashSet<String>();
There are two types of container classes:
- Type of container
- Type of element
(1) . Sequence container:
That is to say, the objects put into the container are arranged in the specified order (order of putting), and multiple objects with the same value are allowed to exist.
package mooc_java.object_container; import java.util.ArrayList; public class NoteBook { // ArrayList (container class) used to store String type private ArrayList<String> notes = new ArrayList<String>(); public void add(String s) { notes.add(s); } public void add(String s, int location) { notes.add(location, s); } public int getSize() { return notes.size(); } public String getNote(int index) { return notes.get(index); } public void removeNote(int index) { notes.remove(index); } 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 static void main(String[] args) { NoteBook nb = new NoteBook(); nb.add("one"); nb.add("two"); nb.add("three", 1); System.out.println(nb.getSize()); System.out.println(nb.getNote(1)); nb.removeNote(0); String[] nb_list = nb.list(); for (String item : nb_list) { System.out.println(item); } } }
(2) . object array
When the element type of an array is a class, each element of the array is actually the manager of the object, not the object itself. Therefore, just creating an array does not create every object in it!
That is to say, after creating an array of elements that are classes, those objects have not yet been generated, and each object in it needs to be created again.
public static void main(String[] args) { String[] a = new String[10]; System.out.println(a[0]); System.out.println(a[0]+"aaa"); System.out.println(a[0].length()); } /* Output results: null nullaaa Exception in thread "main" java.lang.NullPointerException at mooc_java.notebook.NoteBook.main(NoteBook.java:55) */ public static void main(String[] args) { String[] a = new String[10]; System.out.println(a[0]); for (int i = 0; i < a.length; i++) { a[i] = "" + i; } System.out.println(a[0].length()); } /* Output results: null 1 */
For each loop of an array of objects:
package mooc_java.object_container; class Value { private int i; public void set(int i) { this.i = i; } public int get() { return i; } // Any java class that implements the String toString function, // It can be output through system.out.println (object of class Value) public String toString() { return i + ""; } } public class objectArray { // Array loop of type int public static void int_array(int[] array) { for (int i = 0; i < array.length; i++) { array[i] = i; } for (int k : array) { // System.out.println(k); k = 0; } for (int k : array) { System.out.print(k); } } // For each loop of an array of objects public static void object_array(Value[] values) { for (int i = 0; i < values.length; i++) { values[i] = new Value(); values[i].set(i); } for (Value v : values) { v.set(0); } for (Value v : values) { // System.out.print(v.get() + " "); System.out.print(v); } } public static void main(String[] args) { // Array loop of type int int[] array = new int[10]; int_array(array); System.out.println("\t"); // For each loop of an array of objects Value[] values = new Value[10]; object_array(values); } } /* Output results: 0123456789 0000000000 */
(3) . set container:
Set is the concept of set in Mathematics: all elements have unique values, and elements have no order in them.
package mooc_java.object_container; import java.util.ArrayList; import java.util.HashSet; public class container { public static void main(String[] args) { ArrayList<String> strings = new ArrayList<String>(); strings.add("one"); strings.add("two"); strings.add(1, "one"); System.out.println(strings); HashSet<String> stringHashSet = new HashSet<String>(); stringHashSet.add("one"); stringHashSet.add("two"); stringHashSet.add("one"); System.out.println(stringHashSet); } } /* [one, one, two] [one, two] */
(4) . hash table (Hash)
Traditionally, Hash table is a data structure that can store data with int as value. Java's Hash table can store objects as values of any class that implements the hash() function.
Hash table is a very useful data structure. If you are familiar with it and make full use of it, you will get twice the result with half the effort.
package mooc_java.object_container; import java.util.HashMap; import java.util.Scanner; public class Coin { // hash table private HashMap<Integer, String> coinnames = new HashMap<Integer, String>(); public Coin() { coinnames.put(1, "penny"); coinnames.put(5, "dime"); coinnames.put(25, "quarter"); coinnames.put(50, "half-dollar"); System.out.println(coinnames); } public String getName(int amount) { return coinnames.getOrDefault(amount, "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); // Traversal hash table for (Integer key : coin.coinnames.keySet()) { String s = coin.coinnames.get(key); System.out.println(s); } } }