Le byte Java | Package JavaBean, inheritance and permissions modification

This article continues to say Java package. Previous: Le byte Java | GC garbage collection, package and import statements

This tells JavaBean, inheritance and permissions modification

An encapsulating javaBean

Package (Encapsulation) is an important principle of object-oriented approach, is to attributes and behaviors (or methods) objects combined into a single whole, and hide implementation details inside the object as possible.

(1) hide implementation details, provide public access methods

(2) Benefits:

A: hide implementation details, provide public access methods

B: to improve the reusability of code

C: improve security code

 

(3) design principles

I do not want to let the outside world know the implementation details to hide, providing a common access method

 

(4) private is a reflection of the package.

public、protected、private、default

Example 1:

public class Use{
	public static void main(String[] args) { Show.show("封装"); } } //对System.out.println(str)的封装,直接调用就好 class Show{ public static void show(String str){ System.out.println(str); } }

Example 2

public class Man {
	//对属性的封装,一个人的名字,年龄,妻子都是这个对象(人)的私有属性  private String name; private int age; private Woman wife; //对该人对外界提供方法的封装 可以设定妻子的名字,姓名,年龄也可以获得男人的姓名和年龄  //方法封装  public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //此处少了一个getWife()方法,是因为该男人不想让外界访问自己的妻子  public void setWife(Woman wife){ this.wife=wife; } } class Woman{ //属性的封装  private String name; private int age; private Man husband; //方法封装  public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Man getHusband() { return husband; } public void setHusband(Man husband) { this.husband = husband; } }

JavaBean

Itself is a class belonging to the Java object-oriented programming, an object is an instance of the package wrapper classes.
javaBean encoding rules:

(1) javaBean must be declared as public class, so that it can be accessed outside;

In (2) there is at least a JavaBean a constructor with no arguments

(3) all class attributes must be encapsulated, namely: the use of private declaration;

(4) providing a common setter, getter methods private property package;

 

Second, class inheritance and authority control

1, inheritance

Inheritance: inherited his father

He inherited the essence of abstraction. Class is an abstract object, inheritance is a group of abstract classes, in order to achieve a better modeling of the real world. At the same time the use of inheritance can improve the reusability of code. (In fact, the use of combination can achieve better code reuse!) Extends means "expansion." Subclass is an extension of the parent class.

java inheritance mechanism is used, the rules of grammar implementation class extends keyword:

<modifier> class <name> [extends <superclass>]{}

Action: implementation code reuse, extend parent + continuation information.

Through inheritance, the subclass automatically have all the members (member variables and member methods) base class.

Java supports only single inheritance, does not allow multiple inheritance: a subclass can have only one base class, a base class can be derived subclasses

 

2, access control

Java permissions before modifiers define public protected private members placed in the class, used to define access to objects other members of the class of objects.

By controlling access to achieve information hiding

Appreciated: The computer housing using a package, which protects the electrical components, providing a small amount of use of the key abutment therewith. I want to watch TV, simply click on the switch and change channels on it. It is necessary to understand the internal structure of the TV do? CRT it is necessary to bumper?

For convenience we use the manufacturer television, all the details of the complex encapsulated inside, we expose only to simple interface, such as: the power switch. How to achieve specific internal, we do not need to worry about. Another example is your cell phone, the phone's keyboard, screen, and other handset, is its external interfaces. You just need to know how you can use the phone keys, without the need to understand how the internal circuitry of the phone works. Encapsulation mechanism like cell phones will only expose external interfaces without requiring the user to understand its internal implementation. Careful observation, in reality, a lot of things have such characteristics.



We need to let the user know that the exposed, do not need to let the user know all the hidden. This is the package. Vernacular: "The dew dew, the possession of possession."

concept:

Our programming to pursue "high cohesion and low coupling." Internal operational details of the data is high cohesive yourself type, does not allow external interference;

Coupling low: only a small amount of exposure to the external use of the method.



public: at a glance;

protected: inherited his father (his son to use);

default: family harmony;

private: been accounted for.

For the privilege class of modified only with public and default.

public class can access anywhere

default class can only be accessed in the same package

1), the privatization of private property as much as possible

2) access control: setter and getter accessor> private, the majority of the general public

setterXxx: Storage and getterXxx: View

 

Guess you like

Origin www.cnblogs.com/lotbyte/p/11233759.html