20165218 2017-2018-1 "Java Programming" third week study summary

Source: Internet
Author: User
Tags instance method

20165218 2017-2018-1 "Java program design" The third week study summary textbook Learning summary-fourth chapter class and object oriented object language

The first thing to do when you need to do something is to think about who is going to accomplish the task, which object is going to accomplish the task, and what the data is about. ———— "Java 2 Practical Tutorial" P51

Three properties of object-oriented programming: encapsulation, inheritance, polymorphism

    • Encapsulation: encapsulates data and operations on the data. The general concept is formed by abstraction, that is, extracting common properties from specific instances.
graph LRA[行为/功能]-->|抽象|B[方法]
graph LRA[状态描述]-->|抽象|B[属性]
    • Inheritance: subclasses can inherit properties and behaviors of the parent class

    • Polymorphism : One is the polymorphism of the operation name, that is, passing different messages to the operation, and the other is inheritance-related polymorphism, which may produce different behavior when the same operation is called by different types of objects.

Class
    • class = class declaration + class body
    • Contents of the class body: Declaration of variables + method definition
    • The member variable is valid throughout the class, and its validity is independent of the position it is written in the class body.
    • A local variable in a method is only valid within a method and is related to the position it declares .
variables Valid Range
Parameters The whole method
Declared in a compound statement The compound statement
Declared in a loop statement The Loop statement
    • class A{int a = 12; \\合法,声明的同时赋予初值12}
class A{    int a;    a = 12;\\非法}

The contents of the class body cannot have statements, statements can only be present in the method body

Construction method and object creation
    • A special method required by the constructor to create an object with a class , whose name must be exactly the same as the class name in which he is located, has no type .

System.out.println ("The storage capacity of the Nan Fu Battery is:" +nanfu.electricityamount);

类的名字 对象名字 \\声明对象对象 = new 构造方法 \\为对象分配变量(使用new运算符和构造方法)
    • Working with objects
      对象.变量对象.方法
Parameter Passing value
    • Variable parameters
      \\示例public void f(int ... x)\\示例2public void g(double a, int ... x)\\错误示例,可变参数x代表的最后一个参数不是method方法的最后一个参数public void method(int ... x, int y)
      A mutable parameter does not give the name and number of the argument list from the beginning of an item to the last parameter, but the parameter types are the same. A parameter represents a specific parameter in a parameter list that can be represented by the following table operation.
Instance members and class members (static)
    • instance variables and class variables

      instance variables and corresponding object associations, class variables and classes associated

    • Instance methods and class methods
Method instance Method class Method
Assigning an entry address After the class creates the object When the class is added to memory
Call Call through Object Both the class name and the object call can be
Operation Actionable instance variables and class variables Only class variables can be manipulated
this keyword

Represents an object (note that distinguishes between a member variable and a local variable), which can appear in instance methods and construction methods, but not in class methods.

    • The This keyword appears in the constructor method of the class, representing the object created by using the constructor method
    • When the This keyword appears in an instance method, it represents the object that is calling the method
Access rights
Permissions Key Words meaning
Private private In class B, private class variables and private class methods in a are not called by class name of Class A
Total public After a class B is used to create an object, the object can access common variables and common methods in a. class member Variables and class methods in a can also be called through the class name of a in B
The protected protected

After a class B is used to create an object, if B and Class A are in the same package, then the object can access the protected variable and the protected method of a. Any class with a similar package, you can also access a class name protected class variable and protected class method
Friendly | No private , no, public protected retouching | After a class B is used to create an object, if B and Class A are in the same package, then the object can access the friendly variables and friendly methods of a. Class-Friendly member variables and class-friendly methods of Class A can also be accessed by a class name in any class that is similar to a package.

Problems in teaching materials learning and the solving process
    • Question 1: What is the difference between "friendly variables and friendly methods" and "protected member variables and methods"?
    • Problem 1 Solution: Member variables are divided into instance variables and class variables. "Friendly" is "any one class with a similar package, you can access Class A by the class name of class-friendly member variables and class-friendly methods"; "" protected is "any class with a similar package, you can access class A class protected class variables and protected class method". "Friendly" is a bit bigger than protected permission.
Problems in code debugging and the resolution process
    • Issue 1: When it is necessary to compile three source files simultaneously, for example, Example4_8.java Circle.java Circular.java (all under Ch4/src , the current position is Ch4 , at the command line input javac -d bin src/Example4_8.java , the following conditions will appear

    • Problem 1 Solution: This is because you need to compile at the same time, Circle.java Circular.java not Ch4 under, need to enter Ch4/src , use the javac -d ../bin Example4_8. compile can:

    • Question 2: Example of the 76th page of the textbook

    • Issue 2 Solution: Although the class Example4_9 does not declare the data type, but the type is declared in the class SIM , the double SIM data type is uniform when the call assigns a variable to the object simOne . Write letters in the back L .

    • Question 3: Because the understanding is not deep, wrong to write code

    • Problem 3 Solution: Declaring the class is in the format 类 对象名字 , and using the object is, if it is a 对象.变量/方法 class variable, you can 类.类变量 access the class variable directly through the class name

Code Hosting

Resources
    • Java Learning Notes (8th Edition)

    • Java Learning Note (8th Edition) Learning Guide

20165218 2017-2018-1 The third week of the Java Programming Study summary

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.