Getting Started with Java Learning

Getting Started with Java Learning

string

When the java virtual machine starts, it will instantiate 9 object pools to store 8 basic types of wrapper objects and String objects.

Before assigning a value, first determine whether the assigned value exists in the object pool, and if it exists , point to the object ; if it does not exist, create this object.

The assignment of object variables is actually to let b manage the object pool of a

How to create a string
1. Created in the object pool
String str1 = "helloworld";
2. New a String object in memory
String str2 = new String("helloworld");
3. Create a string from an array of characters
char[] chs = {'h','e','l','l','o'};
String str3 = new String(chs);
4. Create a string from a byte array
byte[] byt = {104,101,108,108,111};
String str4 = new String(byt);

Construction method

The setting of incoming parameters for each class

getter and setter functions

Access and set the value of the public variable (?) that sets the class

static

public property

The method after adding static can be called without new

Override @Override

Common overrides toString

@Override
public String toString() {
	return "this:"+ this +"is the new geshi!";
}

array

Array declaration and definition
//常用
int a[];
int a[] = {1,2,3,4};
//用new在内存中开辟一个空间
//声明时必须给定初始长度
int[] b = new int[5];
int[] b = new int[]{1,2,3,4};
array traversal
//普通遍历
for(int i = 0;i < e.length;i ++)
{
	System.out.println(e[i]);
}
//增强遍历?!
for(String str : strs) {
	System.out.println(str);
}
array sort

built-in sorting function

Arrays.sort(i);//其中i为数组名
random number generation
import java.util.Random
//随机数生成类
Random random = new Random();
//生成随机数:[0,100)
int cnt = random.nextInt(100);
Scanner input stream
import java.util.Scanner
//创建Scanner对象:通过系统默认输入流创建
Scanner input = new Scanner(System.in);
//得到需要的对象
String str = input.nextLine();
Two-dimensional array
int [][]a = new int[][];

character type

single character

output, calculation, case conversion

escape character

An unprintable control or special character that begins with a backslash followed by another character

package type

Each base type has a corresponding package type

boolean Boolean
char    Charater
int 	Integer
double	Double

When you need to make a class or object do something, dot operations

boolean isDigit(char a);   //判断字符a是不是数字
isLetter(char a); 	 //判断字符a是不是字母
isLowerCase(char a);
isUpperCase(char a);
isWhitespace(char a);
char toLowerCase(char a);  //转换成小写字符

MathType

abs, pow, random, round (rounded)

function

function definition

A block of code that takes zero or more arguments, does one thing, and returns zero or one value

function call

() plays an important role in representing function calls, and requires no parameters

function parameters

One to one correspondence! Only value can always be passed. Java only

local variable

Every time the function runs, an independent variable space is generated. The variables in this space are unique to this run of the function and are called local variables.

The scope is only in this block and will not be initialized by default.

object

object recognition
Create objects from classes

Objects are entities that need to be created to do things for us

Classes are specifications, and objects are created according to the definition of the class

Example:

Object (the cat): expresses something or an event, responds to a message at runtime

Class (cat): defines all cat properties, types in Java, can be used to define variables

Put the data and the operation on the data together -> encapsulation

define class
  • Object variables are managers of objects
Member variables

The function is called through the object -> point operation

this is a unique fixed local variable

object initialization

The constructor initializes the variables in the object

no return type

Function overloading :

  • A class can have multiple constructors, as long as their parameter tables are different;

  • Other constructors can be called through this();

  • Functions with the same name but different parameter lists in a class form an overloading relationship;

object interaction

A management object manages the required target objects.

Only after new is the object created.

access properties

closed access attribute

private (private): only the inside of this class can be accessed, this restriction is for the class not for the object

open access attribute

public (accessible to all), friendly (accessible in a package), protected

Compilation unit : A .Java file can only have one public class and can have multiple classes (this class can only be accessed in the current compilation unit)

Bag

import imports a class in another package (package name. required class)

or

If you don't import, you can enter the full name of the class

A dot represents a file hierarchy.

kind

class variable
static

Can be accessed by adding . to the name of the class

The variable with this attribute only belongs to this class and does not belong to any object

class method

The function with static added has nothing to do with the object and belongs to this class

Only static functions and variables can be called in the static function (can be called through the object, but not the specific situation of the object)

Notepad example

Determination of needs
interface design

Tips: The division of labor should separate the business logic from the ui

generic container class

ArrayList<String> notes = new ArrayList<string>();

Container type: ArrayList

The type of the element: String

//操作
notes.add(s); // String s;

array of objects

int 
int[] a = new int[10];
//int型的元素

String s;
String[] s = new String[];
//对象数组的每个元素都是对象的管理者而非对象本身

FOR-EACH loop for array of objects

//数据类型
对其操作,不会有变化
//对象数组
for (Value v : a ) {
	对其所管理的
}
//泛型容器也ok

collection container

HashSet<String> s = new HashSet<String>();
//集合的特征:
//元素不会重复

Hash table

HashMap<Integer, String> coinnames = new HashMap<Integer, String>();

//操作
coinnames.put(key, value);
if ( coinnames.containsKey(amount) )
	return coinnames.get(amount);
else
	return "NOT FOUND!";
//同一个键放多个值,只保存最后一次的值
//遍历
for (Integer k : coinnames.keySet() ) {
	String s = coinnames.get(k);
	System.out.println(s);
}

Media Database Design

inherit

What does the subclass inherit from the parent class?

Private in the parent class can be changed to protected, so that subclasses can also access;

or

Create a new constructor for the private variable, and use super(title); for processing;

The definition initialization and constructor execution of the parent class must be completed, and then the definition initialization of the subclass and the execution of the constructor

The relationship between subclasses and parent classes

When the variable in the subclass has the same name as the variable in the parent class, this in the subclass accesses the value of the subclass, and after entering the parent class, it is the value in the parent class

polymorphic variable

subclasses and subtypes

class defines the type

A subclass defines a subtype

Objects of subclasses can be used as objects of superclasses

  • Assign to parent class
  • Passed to containers that expect an object of parent class
  • Put it into the container that stores the parent class object
Subtyping and Assignment
Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();

Objects of subclasses can be assigned to variables of parent class

Subclassing and parameter passing
CD cd = new CD();
database.add(cd);

Objects of subclasses can be passed to functions expecting objects of superclasses

Subtypes and Containers
关系图

relation chart

polymorphic variable
  • Java's object variables are polymorphic, they can hold more than one type of object
  • They can hold objects of the declared type, or objects of subclasses of the declared type
  • Upcasting occurs when an object of a subclass is assigned to a variable of the superclass

shape up

modeling cast
  • Objects of the subclass can be assigned to variables of the parent class, but objects of the parent class cannot be assigned to variables of the subclass

    Vehicle v;
    Car c = new Car();
    v = c;             //可以
    c = v;             //编译错误
    
  • Can be styled

    c = (Car) v;
    (只有当v这个变量实际管理的是Car才行)
    

Tips: Type conversion vs casting

Shape: what to treat

Type conversion: convert to

polymorphism

Binding of function calls

When calling a function through an object variable, which function to call is called binding

  • Static binding: determined according to the declaration type of the variable (know it at compile time)
  • Dynamic binding: Determined according to the dynamic type of the variable (only when the program is executed do you know which function to call)

Calling other member functions in member functions is also called through this object variable (dynamic binding)

cover
  • There are functions with the same name and parameter list in the variables of the subclass and the parent class, and this pair of functions constitutes an overriding relationship

  • When a function with an overriding relationship is called through a variable of the parent class, the function of the class to which the object managed by the variable at that time belongs will be called

Object class

function
  • toString()
  • equal()

castle game design

Eliminate code duplication
encapsulation

Use Encapsulation to Reduce Coupling

  • Both the Room class and the Game class have a lot of code and export related

  • Especially the member variables of the Room class are widely used in the Game class

  • The relationship between classes and classes is called coupling

  • Coupling is as low as possible, keeping distance is the key to good code

Class member variables should be as private as possible

scalability
Aggregation with interfaces
  1. A new method implemented for the Room class, which completely hides the details of the direction inside the Room class
  2. How to achieve the future direction has nothing to do with the outside world
  3. List item

Guess you like

Origin blog.csdn.net/somewon/article/details/128900532
Recommended