10.01_ Object-oriented (overview and role of the package keyword)
- A: Why should I have a bag
* There are many classes in the development, if it is not easy to manage in a folder, and easy to repeat
- Classify byte code (. Class)
- The package is actually a folder
- B: Overview of the Package
Example:
Students: Add, delete, modify, query
Teacher: Add, delete, modify, query
...
方案1:按照功能分 com.heima.add AddStudent AddTeacher com.heima.delete DeleteStudent DeleteTeacher com.heima.update UpdateStudent UpdateTeacher com.heima.find FindStudent FindTeacher方案2:按照模块分 com.heima.teacher AddTeacher DeleteTeacher UpdateTeacher FindTeacher com.heima.student AddStudent DeleteStudent UpdateStudent FindStudent
10.02_ Object-oriented (package definition and considerations) (Master)
- A: Define the format of the package
- Package name;
- Multi-stage ladle. Separate
- B: Considerations for defining packages
- The A:package statement must be the first executable code of the program
- The B:package statement can have only one in a Java file
- C: If there is no package, default means no packages name
- C: Case Demo
- Package Definition and Precautions
Case:
Package Com.heima;
Class Demo1_package {
public static void Main (string[] args) {
System.out.println ("Hello world!");
}
}
10.03_ Object-oriented (class compile and run with package) (master)
- A: How to compile a class that runs a package
- A:javac when compiling with-D
- B: Execute via Java command.
- The Java package name. Hellword
10.04_ Object-oriented (access between classes under different packages) (master)
- A: Case Demo
- Access between classes under different packages
Case:
Package Com.heima;
Class Demo1_package {
public static void Main (string[] args) {
Com.baidu.Person p = new Com.baidu.Person ("Zhang San", 23);
System.out.println (P.getname () + "..." + p.getage ());
}
}
Package Com.baidu;
public class Person {
private String name;
private int age;
Public person () {}
Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
public void Setage (int.) {
This.age = age;
}
public int getage () {
return age;
}
}
10.05_ Object-oriented (overview and use of the Import keyword) (master)
- A: Case Demo
- Why should have import Ctl,shif+o also can guide package
- In fact, the class that has the package is visible to the caller without having to write the full class name.
* The Person class after the guide is visible to the Demo1_package
- B: Guide Package format
- Import package name;
- Attention:
- This way the import is to the name of the class.
- Although it can be written last *, but not recommended.
- C:package,import,class there is no sequential relationship (interview) package must be the first sentence, only one, then the import, can have multiple sentences, and then the class
Case:
Package Com.heima;
Import Com.baidu.Person;
Import com.xxx.Student;
Import Java.util.Scanner; In development, we used to import specific classes
Import Java.util.; //On behalf of the wildcard, he will go to the package to match each other, the match on the import
Class Demo1_package {
public static void Main (string[] args) {
Person p = new person ("Zhang San", 23);
System.out.println (P.getname () + "..." + p.getage ());
P.print (); The unrelated classes under different packages are not allowed to access, because they are protected decorated
/Scanner sc = new Scanner (system.in);
int x = Sc.nextint ();
SYSTEM.OUT.PRINTLN (x);/
}
}
Package Com.baidu;
public class Person {
private String name;
private int age;
Public person () {}
Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
public void Setage (int.) {
This.age = age;
}
public int getage () {
return age;
}
}
10.06_ Object-oriented (test of four permission modifiers) (master)
* Package: Hide implementation Details, provide public access to external
- A: Case Demo
- Four types of permission modifiers
- B: Conclusion
本类 同一个包下(子类和无关类) 不同包下(子类) 不同包下(无关类)private Y 默认 Y Yprotected Y Y Ypublic Y Y Y Y
Case:
Package Com.heima;
Import Com.baidu.Person;
Import com.xxx.Student;
Import Java.util.Scanner; In development, we used to import specific classes
Import Java.util. ;// on behalf of the wildcard, he will go to the package to match each other, the match on the import
Class Demo1_package {
public static void Main (string[] args) {
Person p = new person ("Zhang San", 23);
System.out.println (P.getname () + "..." + p.getage ());
P.print (); The unrelated classes under different packages are not allowed to access, because they are protected decorated
/Scanner sc = new Scanner (system.in);
int x = Sc.nextint ();
SYSTEM.OUT.PRINTLN (x); /
Student s = new Student("李四",24);System.out.println(s.getName() + "..." + s.getAge());s.method();
}
}
Package Com.baidu;
public class Person {
private String name;
private int age;
Public person () {}
Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
public void Setage (int.) {
This.age = age;
}
public int getage () {
return age;
}
protected void print () {
System.out.println ("print");
}
}
Package com.xxx;
Import Com.baidu.Person;
public class Student extends person {
Public Student () {}
Public Student (String Name,int age) {
Super (Name,age);
}
public void Method () {
Print ();
}
}
10.07_ Object-oriented (common modifiers used by classes and their components) (master)
10.08_ Object-oriented (internal class overview and Access Features) (learn)
- A: Internal class overview
- B: Internal class Access features
- A: The inner class can access the members of the external class directly, including the private.
- B: The outer class must create an inner class object to access the members of the inner class. But if the private object of the inner class, the outer class is inaccessible
- The external class name. Internal class Name Object name = External Class object. Inner class object;
- C: Case Demo
- Internal class extremely accessible features
Case:
Class Demo1_innerclass {
public static void Main (string[] args) {
Inner i = new Inner ();
I.method ();
The outer class name. internal Class name = External Class object. Inner Class object
Outer.Inner oi = new Outer (). New Inner (); Create an inner class object so that the external class object also creates a
Oi.method ();
}
}
Class Outer {
private int num = 10;
Class Inner {
public void Method () {
SYSTEM.OUT.PRINTLN (num);
}
}
}
10.09_ Object-oriented (private use of member inner classes) (learn)
- Private: You cannot create an inner class object directly outside the
Case:
Class Demo2_innerclass {
public static void Main (string[] args) {
Outer.Inner oi = new Outer (). New Inner ();
Oi.method ();
Outer o =
New Outer ();
O.print ();
}
}
Class Outer {
private int num = 10;
Private class Inner {//This time can not be used in other classes Inner class, private
public void Method () {
SYSTEM.OUT.PRINTLN (num);
}
}
public void print () {
Inner i = new Inner ();
I.method ();
}
}
10.10_ Object-oriented (static member inner Class) (learn)
- Static
- B: The member inner class is accessed by statically modifying the following methods:
- The external class name. Internal class Name Object name = External class name. Inner class object;
Outer.Inner oi = new Outer (). New Inner (); This is in the ordinary member class, used to two new actually created two objects, an inner class object, an external class object, notice that there are two () called two classes of constructors, respectively,
Outer.Inner oi1 = new Outer.Inner (); This is only possible with the class name in a static member class, and in this case only an inner class object is created, and no external class object is created because only a new
Case:
Class Demo1_innerclass {
public static void Main (string[] args) {
The external class name. Internal class Name Object name = External class name. Inner class object;
Outer.Inner oi = new Outer.Inner ();//The static inner class must be created, in fact, only the inner class object is created;
Oi.method ();
Outer.Inner2.print ();//class name plus. The way to do
}
}
Class Outer {
Static Class Inner {
public void Method () {
System.out.println ("method");
}
}
Static Class Inner2 {
public static void print () {
System.out.println ("print");
}
}
}
10.11_ Object-Oriented (interview questions for members ' inner classes) (master)
Class Outer {
public int num = 10;
Class Inner {
public int num = 20;
public void Show () {
int num = 30;
System.out.println (?); /num
System.out.println (??); /this.num
System.out.println (???); /outer.this.num, an inner class is able to access an external class member because it can obtain a reference to an external class outer.this,outer the num of the following this
}
}
}
Class Innerclasstest {
public static void Main (string[] args) {
Outer.Inner oi = new Outer (). New Inner ();
Oi.show ();
}
}
10.12_ Object-oriented (local internal class accesses local variable problem) (master)
A: Case Demo
- Local internal class access local variables must be final decorated
Local inner class in accessing the local variables in his method must be final decorated, why?
Because when this method is called, if the local variable is not final decorated, his life cycle and method life cycle is the same, when the method of the stack, the local variable will disappear, then if the local inner class object has not disappeared immediately want to use this local variable, there is no, If the final decoration will enter the constant pool when the class loads, even if the method is stacked, the constant pool constant is still there, you can continue to use
But jdk1.8 canceled this thing, so I think it's a bug.
Case:
Class Demo1_innerclass {
public static void Main (string[] args) {
Outer o = new Outer ();
O.method ();
}
}
Local inner class
Class Outer {
public void Method () {
final int num = 10;
Class Inner {//local inner class and local variable, only valid in method, out of method is invalid
public void print () {
SYSTEM.OUT.PRINTLN (num);
}
}
Inner i = new Inner ();
I.print ();
}
/Publicvoid run () {
Inner i = new Inner (); Local inner class, accessible only in the method in which it resides
I.print ();
}/
}
10.13_ Object-oriented (the format and understanding of anonymous inner classes)
10.14_ Object-oriented (anonymous inner class overrides multiple method calls)
- A: Case Demo
- method Invocation of anonymous inner class
Case:
class Demo2_nonameinnerclass {
public static void Main (string[] args) {
Outer o = new O Uter ();
O.method ();
}
}
interface Inter {
public void show1 ();
public void Show2 ();
}
//Anonymous inner class is only used when overriding a method, otherwise it's too cumbersome
class Outer {
public void method () {
/ new Inter () {
public void Show1 () {
System.out.println ("Show1");
}
public void Show2 () {
System.out.println ("Show2");
}
}.show1 ();
New Inter () {
public void Show1 () {
System.out.println ("Show1");
}
public void Show2 () {
System.out.println ("Show2");
}
}.show2 (); /
Inter i = new Inter () {
public void Show1 () {
System.out.println ("Show1");
}
public void Show2 () {
System.out.println ("Show2");
}
/ public void show3 () {
System.out.println ("show3");
} /
};
I.show1 ();
I.show2 ();
//i.show3 ();//Anonymous inner class cannot be transformed downward because there is no subclass class name
}
}
10.15_ Object-oriented (application of anonymous inner class in development)
- A: The code is as follows
*
Write abstract classes Here, interfaces are OK
Abstract class Person {
public abstract void Show ();
}
class PersonDemo { public void method(Person p) { p.show(); } } class PersonTest { public static void main(String[] args) { //如何调用PersonDemo中的method方法呢? PersonDemo pd = new PersonDemo (); //请在此处补充代码 } }
Answer:
Class Test1_nonameinnerclass {
public static void Main (string[] args) {
How do I call method methods in Persondemo?
Persondemo PD = new Persondemo ();
Pd.method (New Student ());
Pd.method (new person () {
public void Show () {
System.out.println ("show");//The Anonymous object class is passed as a parameter
}
});
}
}
Write abstract classes Here, interfaces are OK
Abstract class Person {
public abstract void Show ();
}
Class Persondemo {
//public void method(Person p) { //Person p = new Student(); //父类引用指向子类对象/*Person p = new Person(){ public void show() { System.out.println("show"); }};*/public void method(Person p) { p.show();}
}
Class Student extends Person {
public void Show () {
System.out.println ("show");
}
}
10.16_ Object-oriented (anonymous inner class interview questions)
}
Class Outerdemo {
public static void Main (string[] args) {
Outer.method (). Show ();
/* Analysis: The method method must be in the outer, and the return value of this approach must be the implementation class object of ITER
*/
} }
}
Required to output "HelloWorld" on the console
Answer:
Class Test2_nonameinnerclass {
public static void Main (string[] args) {
Outer.method (). Show (); Chained programming, after each call to the method can continue to invoke the method, proving that the calling method returns an object,. Show instructions call this method, then this method must be rewritten
Inter i = Outer.method ();
I.show ();
}
}
Interface Inter {
void Show (); This is an interface that must be overridden to call this method
}
Class Outer {
The code is padded
public static Inter method () {///return value must be a Inter object, to invoke show, must be overridden for show
return new Inter () {//Anonymous class object format:
public void Show () {
System.out.println ("HelloWorld");
}
};
}
}
Required to output "HelloWorld"//On the console, as required, to complement the code
10.17_DAY10 Summary
- Summarize today's knowledge points once again.
From for notes (Wiz)
10 Object-oriented (overview and role of the package keyword)