On the trap problem of variable parameter and method overload:
(1) Error: cannot declare method(int []) and method(int) in Test02Problem at the same time )
(2) Error: cannot declare method(int a, int args) and method(int args)
Because the above situation will lead to the situation that both methods match
class Problem{ public static void main(String[] args){ method(1); method(1,2);//Error: ambiguous reference to method //method(int,int...) in Test02Problem2 and method(int...) in Test02Problem2 match } public static void method(int a){ //.... } public static void method(int a, int... args){ //.... } public static void method(int... args){ //.... } }
1, Recursive
1. Concept:
When a method calls itself directly or indirectly, it is called recursion.
2. How to write recursion
Question:
If it can't be written, StackOverflowError will appear, which means stack memory overflow.
Because every call, there will be a "stack", that is, to open up a separate memory space for this method.
How to write?
(1) We must guarantee that there is an exit, that is, conditional recursion, and that there is an end condition
(2) Although there are restrictions in recursion, the number of recursions cannot be too many
3. Examples
1. For example: find the sum of 1-100
(1) Use cycle
(2) Using recursion
If I know the sum of 1-99, I can use the sum of 1-99 and + 100 to get the sum of 1-100
Similarly, I know the sum of 1-98, and I can use the sum of 1-98 and + 99 to get the sum of 1-99
Similarly,...
Similarly, I know the sum of 1-2, I can use the sum of 1-2, and then + 3, I get the sum of 1-3
How to find the sum of 1-2? I can use 1 + 2
Pushed:
The sum of 1 to n-1 + n gives the sum of 1-n
class DiGui{ public static void main(String[] args){ int he = sum(100); System.out.println("he=" + he); } //Method: if sum(n) is the sum of 1 to n = = //Find sum(n-1) of 1 to n-1, and then add n to it to get sum of 1 to n public static int sum(int n){ //When n=1, stop calling itself if(n==1){ return 1; } return sum(n-1) + n; } /* sum(100) ==> sum(99) + 100 sum(99) ==> sum(98) + 99 sum(98) ==> sum(97) + 98 ... sum(2) ==> sum(1) + 2 sum(1) ==> 1 */ } /* class Test03DiGui{ public static void main(String[] args){ a();//java.lang.StackOverflowError,Stack memory overflow } public static void a(){ a();//Call yourself directly } public static void b(){ c();//Indirect recursion } public static void c(){ b(); } }*/
Object array:
Array: a container containing a set of elements of the same data type. Previous arrays: int[],char [], etc. they contain a set of basic data type values. Today's arrays: String [], Circle [], Student [], etc. they contain a set of objects.
Remembers:
1. About arrays
(1) How to declare a one-dimensional array
Array name of data type [] of element;
(2) Initialization of arrays
A: Static initialization
Array name = data type of new element [] {value of element};
or
Data type of element [] array name = {value of element};
B: Dynamic initialization
Array name = data type of new element [length]; / / at this time, the element is the default value
Array name [subscript] = value;
(3) How to get the length of an array
Array name. length
(4) Accessing elements of an array
Array name [subscript]
(5) Default value of element
byte,short,int,long: 0
float,double: 0.0
char: \u0000
boolean:false
Reference data type, null
2. Variable assignment requirements:
The type of the value is consistent or compatible with the type of this variable:
Agreement:
int a = 10;
double d = 1.2;
String str = "hello";
char c = Shang;
compatible
double d = 12;
char c = 97; / / in encoding range
For variables of data type, only objects can be assigned.
Student stu = new Student();
Student other = stu;
3. About accessing member variables and member methods in other classes
(1) Static member variable, member method
Class name. Static member variable
Class name. Static member method
Object name. Static member variable
Object name. Static member method
Anonymous objects. Static member variables
Anonymous objects. Static member methods
(2) Non static member variable, member method
Object name. Non static member variable
Object name. Non static member method
Anonymous objects. Non static member variables
Anonymous objects - non static member methods
As long as it is an object name or an anonymous object, it can. Its member variables and member methods.
class bjectArray{ public static void main(String[] args){ //A student object Student stu = new Student(); stu.name = "Zhang San"; stu.score = 89; System.out.println(stu.getInfo()); //Objects to store a group of students //(1) Declare an array and initialize it to a length of 5 //Because the objects to be stored in the array are students' objects, the element type is Student Student[] arr = new Student[5];//At this time, the element is the default value, Student is the reference data type, null //System. Out. Println (arr [0]. Getinfo()); / / NullPointerException arr [0] is null //arr[0] = value; / / arr[0] on the left is of Student type and can only be assigned as an object on the right arr[0] = stu;//stu stores the address of a student object. It copies the address to arr[0] System.out.println(arr[0].getInfo()); arr[1] = new Student();//Create a new object and assign it to arr[1] arr[1].name = "Li Si"; arr[1].score = 78; System.out.println(arr[1].getInfo());//At this time, arr[0],arr[1] is the object name } } //(1) State the type of student first class Student{ String name; int score; String getInfo(){ return "Full name:" + name + ",Results:" + score; } }
-
Learned the basic concept of object-oriented
Learning the concept of class and object
Learned the class declaration format and object creation format
Learned two members of the class: member variable and member method
Learned about object arrays
1, The basic characteristics of object-oriented
1. The basic characteristics of OO are encapsulation, inheritance and polymorphism
The characteristics of OO are encapsulation, inheritance, polymorphism and abstraction
2. Encapsulation:
Why encapsulation?
(1) Hide the details of internal implementation,
① For users, you don't need to know too much. The less you know, the easier it is to operate
② Safer and more controllable for designers
(2) Internal information is intercommunication
To sum up, high cohesion and low coupling
The hidden, the exposed
3. Encapsulation boundary
(1) In this category
(2) In this package
(3) This module (introduced by Java9)
(4) This system
4. How to achieve encapsulation?
Control according to authority modifier, etc
This class subclass of this package other package non subclass of other package other module
private √ × × × ×
Default (don't write) √×××
protected √ √ √ × ×
public √√√ not allowed by default
5. Which permission modifiers can be used to control its visibility?
In other words, what can a permission modifier modify?
External class: public or default
Member variables: private, default, protected, public
Member methods: private, default, protected, public
. . .
6. Member variable privatization
(1) Add private before member variable
(2) If necessary, you can provide the get/set method
Set method: set the value. Call the set method to modify the value of a member variable
Get method: get the value. Call get method to get the value of a member variable
class Encapsulation{ public static void main(String[] args){ Student stu = new Student(); //stu.name = "Zhang San"; / / error, name is privatized, not allowed to be used outside the Student class //stu.setName = "Zhang San"; / / error stu.setName("Zhang San"); System.out.println("Full name:" + stu.getName()); stu.setScore(-100); System.out.println("Results:" + stu.getScore()); } /* Internal class, later class Inner{ } */ } class Student{ private String name; private int score; //Assign a value to name using the value of parameter n public void setName(String n){ name = n; } public String getName(){ return name; } public void setScore(int s){ if(s>=0){ score = s;//Make the assignment of score controllable } } public int getScore(){ return score; } }
Two, bag
1. Why do you need a bag?
(1) Avoid duplicate names of classes
Full name of class: package. Class name
For example: java.lang.String
java.util.Scanner
java.lang.Math
java.lang.System
(2) Control the visibility range of some classes or members
If the permission modifier of a class or member is the default, it is only used by this package
(3) It is convenient for organizations to manage many classes
In the JRE core class library:
java.lang: core language pack
java.io: related to input and output
java.net: related to network programming
java.util: related to tool class
java.sql: related to database operation
java.text: related to text processing
java.time: related to date and time
...
2. How to declare a package
package name;
Must be at the first line of the. java source file. A. java source file can only have one package statement;
3. How to name a package
(1) All words are lowercase. Use between words. Split
(2) You can't use the java keyword to reserve words, and you don't need to use java
(3) Generally, it is customary to use company domain name inversion + business module name;
For example:
com.atguigu.bean;
com.atguigu.service;
com.atguigu.util;
Primary domain name: com (business), org (Organization), edu (Education), gov (government)
4. How to compile and run it? (understanding)
(1) Compile
Original: javac source filename.java
Now: javac-d. source filename. java
-d: Create package directory structure
.: create package directory structure in current directory
(2) Operation
Original: java class name
Now: java package name. Class name
5. How do I use classes from other packages?
Premise: the classes of other packages must be visible in this package, and the modifier is > default
(1) Use full name
java.util.Scanner input = new java.util.Scanner(System.in);
com.atguigu.bean.Teacher t = new com.atguigu.bean.Teacher();
(2) Use import statement + short name
A: import package. Class name;
B: import package. *;
Note: when using two different packages of the same name class, what to do?
For example: java.util.Date
java.sql.Date
One with import and one with full name
package com.atguigu.pkg; import com.atguigu.bean.Teacher; import java.util.*; import java.util.Date; //import java.sql.Date; class Package{ public static void main(String[] args){ System.out.println("hello package"); //This Teacher class is under the com.atguigu.bean package com.atguigu.bean.Teacher t1 = new com.atguigu.bean.Teacher(); com.atguigu.bean.Teacher t2 = new com.atguigu.bean.Teacher(); com.atguigu.bean.Teacher t3 = new com.atguigu.bean.Teacher(); Teacher t4 = new Teacher(); Scanner input = new Scanner(System.in); Date d = new Date(); java.sql.Date d2 = new java.sql.Date(1000); } } class Student{ }