Dahne Training Object-oriented fourth day notes

Source: Internet
Author: User
Tags arrays modifier modifiers
Review:
1. Object Memory Management
2. Inheritance
3.super
4. Upward styling


Q: Can the construction method inherit?----?
The constructor method of the parent class is not inherited, but is called.
Class aoo{
int A;
Aoo () {
}
void Show () {}
}
Class Boo extends aoo{
int b;
Boo (int b) {
Super ();
this.b = b;
}
void Say () {}
}


Boo o = new Boo (); Compile error














Notes:
1. Rewriting of methods: Overwrite, change
2. Override (override) and overload (overload) Difference:----Common face questions
1) Rewrite: parent-child class, method name is the same, parameter list is same, method body is different
Run-time bindings, based on object binding methods (see objects)
2) Overloading: Method name is the same, parameter list is different
Compile-time binding, depending on the type of parameter binding method (see Type)
2.package:
1) Role: Avoid naming conflicts
2) can have hierarchy, package name suggested all letters are lowercase
3) classes in the same package can be accessed directly
Classes in different packages:
3.1) First import Declaration/Introduction class, direct access to----recommendations
3.2) Fully qualified name: Package name. class name-------not recommended
Import
1) Role: Declaring classes, introducing classes
2) Syntax: Import package name. class name;
3. Access Control Modifiers:
1) Public: Common, can be anywhere
2) Protected: Protected, this class, sub-class, same package class
3) Default: Do not write anything, this class, the same package class
4) Private: proprietary, this class
Class can only be decorated with public or default
Members in a class can be decorated with the above 4
4.static: Static
1) Modifying member variables-----static variables
1.1) belongs to the class and not to the object
1.2) and the information of the class is stored in the method area together with only one copy of the
1.3) often accessed through the class name.
1.4) When to use: all objects have the same values
2) Modification method--------static method
2.1) No implicit this pass
2.2) Instance members (instance variables, methods) cannot be accessed directly in a static method
You can access static members directly (static members do not require objects to access, class names.)
2.3) often accessed through the class name.
2.4) When to use: the operation of the method is not related to the object, but only to the parameter
3) modifier block---------static block
3.1) execution during class loading
Because the class is only loaded once, the static block is executed only once
3.2) When to use: Load static resources (Pictures, audio, video ...)
5.final: The final
1) modifier variable: variable cannot be modified
2) Modification Method: Method cannot be rewritten
3) Modifier class: class cannot be inherited




Task:
1. Presentation method overrides a large (access permission) and a small (return value type)


Homework:
1. Theoretical questions
2. Operation problem
3. Open a Notepad:
Access modifier Small Code
Static Small Code
Final Small Code








Textbook 86 page last ppt (static final constant)------self-study














Finaldemo


CS Games-------Map
Map this data, all objects are the same, so make static variables (static resources)
Static blocks, assigning values to static resources












Static method: The operation of the method is only relevant to the parameter, not to the object
Double A = Math.random ();
Double b = math.sqrt (25);
int[] A1 = arrays.copyof (a,6);
Arrays.sort (arr);


static void sort (int[] arr) {
Just ascending to Arr
}












Math m1 = new Math ();
M1.SQRT,-----5


Math m2 = new Math ();
M2.SQRT,-----5


Math m3 = new Math ();
M3.SQRT,-----5




The access member variable in the method is preceded by a this.
The normal method has a pass by default.
Static methods do not have an implicit this pass


0:this represents the current object
1. Instance members must pass through the object. To access
2. Static method does not have an implicit this pass
Without this, it means that there are no objects
No object, which means that instance members cannot be accessed directly


Cell C1 = new cell (2,5);
C1.drop ();


Cell C2 = new cell (3,6);
C2.drop ();


The operation of the drop () method is related to the object, so it cannot be static.
















Class cell{
int row;
int col;
static int A;
void Drop () {//has a default this passed over
}
void MoveLeft () {
}
void MoveRight () {
}
String Getcellinfo () {
}

static void Show () {//without this pass
row++; Compile error (row can only be accessed by object.)
a++; Correct (a can be accessed by the class name, no objects required)
}
}
Cell C1 = new cell ();
C1.drop ();






















Class cus{
String cardId;
String cardpwd;
Double balance;
static double Peri;
}
Cus.peri = "0.65";


















Class student{
String name;
int age;
String address;
Static String ClassName;
}
Student.classname = "JSD1503";


Student ZS = new Student ();
Zs.name = "Zhangsan";
Zs.age = 25;
zs.address = "Langfang";


Student ls = new Student ();
Ls.name = "Lisi";
Ls.age = 26;
ls.address = "Shandong";


















Staticdemo








There are two types of member variables:
1. Instance variables: not modified by static
2. Static variable: modified by static


Class aoo{
int A; Instance variable
static int b; static variables
}


AOO.B = 1;


Aoo o = new Aoo ();
O.A = 1;


Method area: Class (. Class), method, static variable (1 copies)
Aoo O1 = new Aoo ();












Syntax rules: Classes can only be used with public and default
Members in a class can have 4 access modifiers


Nested classes-----Classes in the inner class
Class aoo{
Class boo{//Internal classes
}
}








Oo.day04.vis






First time----wrong.
The second time----wrong.


Q: What is my password?


PWD password
public boolean checkpwd (String pwd) {
..... A bunch of validations
return true/false;
}


Supermarket shopping-------250 yuan


Not only to protect the data, but also often to protect the implementation of a function of the process






Package P1;
Class aoo{
}


Package p2;
Class aoo{
}


Package P3;
Import P1. Aoo;
Import P2. Aoo;
Class test{
void Show () {
P1. Aoo O1 = new P1. Aoo ();
P2. Aoo O2 = new P2. Aoo ();
}
}












Package day02;
Import Java.util.Scanner;
Import Java.util.Arrays;
public class test{
Main () {
Scanner scan = new Scanner (system.in);
int num = Scan.nextint ();

Arrays.sort (arr);

}
}














Minsheng Bank-----Billions of dollars in Java to do a set of software


A company-----a module
B Company-----B Module
C Company-----C module


Suppose a company project
ABC------Aoo
Suppose B Company Project
ABC------Aoo


Naming recommendations for package names:
Domain name anti-write. The project name. Module name
Com.tarena. Studentmanager. Teachermana
Com.tarena. Studentmanager. Studentmana
The company does not conflict with the project conflict module does not conflict

Classes in the same package that can be accessed directly
Classes in different packages cannot be accessed directly:
1.import Introduction to class----recommendations
2. Fully qualified name----too long, not recommended

Package P1;
Class aoo{
}
Class boo{
void Show () {
Aoo o = new Aoo ();
}
}


Package p2;
Import P1.  Aoo; Declaring the Aoo class, referencing the Aoo class
Class coo{
void Show () {
Aoo o = new Aoo (); Fully qualified name
}
}
























Overloaded look type, overriding look object
void A (Coo o) {
O.show (); Tune the doo
}
void A (Doo o) {
O.show ();
}


Coo o = new Doo (); Upward styling
A (O); ------The first one






1000 class------cannot have a name
Java recommendation to put classes in a package
The class in the same package cannot have the same name


















. Java----. Class
At compile time, there is no object's










Overrideoverloaddemo






Student ZS = new Student ();
Zs.name = "Zhangsan";
Zs.age = 25;
zs.address = "Langfang";
Zs.classname = "JSD1503";
Zs.sayhi ();------name, age, address


Teacher wkj = new Teacher ();
Wkj.name = "wangkejing";
Wkj.age = 26;
wkj.address = "Jiamusi";
Wkj.salary = 5000;
Wkj.sayhi ();------name, age, address


When the overridden method is called, look at the object


person P1 = new person ();
P1.sayhi (); ----Person's


Student stu = new Student ();
Stu.sayhi (); ----student.


person P2 = new Student (); Upward styling
P2.sayhi (); ----student.




Class person{
String name;
int age;
String address;
void Say () {
System.out.println (name+ "," +age+ "," +address ");
}
}
Class Student extends person{
String ClassName;
void Say () {
System.out.println (name+ "," +age+ "," +address+ "," +classname ");
}
}
Class Teacher extends person{
Double salary;
void Say () {
System.out.println (name+ "," +age+ "," +address+ "," +salary ");
}
}
Class Doctor extends person{
String level;
}























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.