java object-oriented design core - Classes and Objects

Object-oriented learning three main lines

  1. and members of the class java class
    attributes, methods, constructors; code block, inner class
  2. Three features of object-oriented
    encapsulation, inheritance, polymorphism (abstract)
  3. Other keywords
    this, super, static, final, abstract, interface, package, import , etc.

    Process for the (POP) and object-oriented (OOP)

     二者都是一种思想,面向对象是相对于面向过程而言。面向过程,强调的是功能行为,以函数为最小单位,考虑怎么做。面向对象,将功能封装进对象,强调具备功能的对象,以类/对象为最小单位,考虑谁来做。
     面向对象更加强调运用人类在日常的思维逻辑中采用的思想方法与原则,如抽象、分类、继承、聚合多态等。

    Three basic features of object-oriented programming

    Encapsulation (Encapsulation)
封装是一种信息隐蔽技术,它体现于类的说明,是对象的重要特征。封装把数据和加工该数据的方法(函数)打包为一个整体,以实现独立性很强的模块,使得用户只能见到对象的外特性(对象能接受哪些信息,具有哪些处理能力),而对象的内特性(保存内部状态的私有数据和实现加工能力的算法)对用户是隐蔽的。封装的目的在于把对象的设计者和对象的使用者分开,使用者不必知晓其行为实现的细节,只需要用设计者提供消息来访问该对象

Inheritance (Inheritance)

继承性是子类共享其父类数据和方法的机制他由类的派生功能体现。一个类直接继承其他类的全部描述,同时可以修改和扩充。继承具有传递性。类的对象是各自封闭的,如果没有继承性机制,则类中的属性(数据成员)、方法(对数据的操作)就会出现大量重复。继承不仅支持系统的可重用性,而且还促进系统的可扩展性。

Polymorphism (Polymorphism)

对象通常根据接受到的消息做出动作。当同一消息被不同的对象接受并产生不同的行动,这种现象称为多态性。例如:同样是run方法,鸟调用时是飞,野兽调用时是奔跑。
利用多态性,用户可以发送一个通用信息,而将所有的实现细节都留给接受消息的对象自行决定。

The basic concept of classes and objects

类和对象是面向对象的核心概念
类是对一类事物的描述,是抽象的、概念上的定义
对象是实际存在的该类事物的每个个体,也称为实例

class

    类是一个抽象的概念,要利用类的方式解决问题,必须用类创建一个实例化的对象,然后通过对象去访问类的成员变量,去调用类的成员方法来实现程序的功能。比如“汽车”是一个抽象的概念,只有使用一辆具体的汽车才能感受汽车的功能。
    一个类可以创建多个类的对象,它们具有相同的属性模式,但可以具有不同的属性值。java程序为每个对象都开辟了内存空间,已保存各自的属性值。

Objects

    对象(object)是类的实例化后的产物。对象的特征分为静态特征和动态特征。
    静态特征是指对象的外观、特质、属性等。动态特征是指对象具有的功能、行为等。
    人们将对象的静态特征抽象为属性,用数据来描述,在JAVA语言中称之为成员变量。而将动态特征抽象为行为,用一组代码来表示,完成对数据的操作,在JAVA语言中称之为方法(Method)。
    一个对象是由一组属性和一类属性进行的操作(即方法)构成的。

And the definition of the class declaration

Limit modifier

在java中提供了四种访问权限,使用不同的访问权限修饰符修饰时,被修饰的内容会有不同的访问权限
  1. public: public
  2. protected: Protected
  3. default: default
  4. private: private
public protected default (empty) private
The same class
The same package (regardless of class and subclass)
Subclasses of different packages
In different packages regardless of the class

public is the ultimate authority. private the least privilege
when writing code, if there are no special considerations, the proposed use of this authority:

  1. Member variables private, hidden details
  2. Constructor uses public, easy to create objects
  3. Members method uses public, easy to call the method

Class declaration

[标识符] class 类名称
{
    //类的成员变量
    //类的方法
}
声明类使用的class关键字。声明一个类时,在class关键字后面加上类的名称,
标识符可以是public、private、protected或者完全省略,类名称只要是一个合法的表示符即可,但从程序的可读性方面来看,类名称建议是由一个或多个有意义的单词连缀而成,形成自我注释(SelfDocumenting),每个单词首字母大写,单词间不要使用其他分隔符。

The composition of the class

class Person {
    String name;    
    int age;    
    void talk(){        
        System.out.println("我是"+naem+",今年"+age+"岁");    
        }
}
首先用class声明了一个名为Person的类,在这里Perosn是类的名称
第02和03行声明了两个属性(即描述数据的变量)name和age,name为String(字符串类型)
型,age为int(整型)型。
第04~06行声明了一个talk()方法----操作数据(如name和age)的方法,此方法用于向屏幕打印信息。

Class definition

    对于一个类而言。构造方法、属性和方法,是常见的3种成员。可以定义零个或者多个。如果3中成员都只定义零个,实际上这是一个空类。
    各个成员之间定义的先后顺序没有任何影响。各个成员可以互相调用,但是static修饰的成员不能被非static修饰的成员访问。

Class definition to use

public class ColorDefine {
    String color = "黑色";    
    void getMes(){        
        System.out.println("定义类");    
    }    
    
    public static void main(String[] args) {        
        ColorDefine b = new ColorDefine();
        System.out.println(b.color); //黑色
        b.getMes();  //定义类    
        }
    }

Class of property

Defined properties
[Modifier] property type name [= default]

  1. Modifier: modifier can be omitted, use the default access default like, may also access the display of controls public, protected, private and static, final, of which three access specifier public, protected, and wherein the private use only a, static and final modifications can be combined properties.
  2. Attribute Type: JAVA attribute type may be permitted to any data type, including the basic type (int, float, etc.) and reference types (Interface, an array, etc.)
  3. Property name: From the perspective of syntax, the attribute name is a valid identifier as long as you can. However, if the attribute name should consist of one or more words put together meaningful from the perspective of readability made, first recommended at the beginning of the second uppercase lowercase word beginning, do not use separator between all the rest lowercase words . As String studentNumber
  4. Default: attribute definitions can also define an optional default
属性是一种比较符合汉语习惯的说法,在JAVA官方文献中,属性被称为Field,因此有些书籍也把“属性”翻译为“字段”或“域”,它们在本质上是一样的

Use of property

public class usingAttribute {
    //定义两个String类型的属性str1和str2,它们是静态的,所以它们是属于类的,也就是属于这个类定义的所有对象共有,对象看到的静态属性值都是相同的
    static String str1 = "string-1";
    static String str2;

    //定义了两个String类型的属性str3和str4,因为它们是非静态的,所以它们是属于这个类所定义的对象私有的,每个对象都有这个属性,且各自的属性值可不同
    String str3 = "string-3";
    String str4;

    //定义了静态方法块,它没有名称。使用static关键字加以修饰并用大括号“{}”括起来称之为静态代码块用来初始化静态成员变量。如静态变量str2被初始化为“string-2"
    static{
        printStatic("before static");
        str2 = "String-2";
        printStatic("after static");
    }

    //定义一个方法用来打印静态成员变量
    public static void printStatic(String title){
        System.out.println("--------"+title+"---------");
        System.out.println("str1=\""+str1+"\"");
        System.out.println("str2=\""+str2+"\"");
    }

    //定义一个构造方法usingAttribute(),在这个方法中,使用了类中各个属性。构造方法与类同名,且无返回值(包括void),它主要的目的是创建对象。
    //打印一次属性,然后改变str4属性,最后再打印一次
    public usingAttribute(){
        print("before constructor");
        str4 = "string-4";
        print("after constructor");
    }

    //定义公有方法用来打印所有属性,包括静态成员
    public void print(String title){
        System.out.println("--------"+title+"--------");
        System.out.println("str1=\""+str1+"\"");
        System.out.println("str2=\""+str2+"\"");
        System.out.println("str4=\""+str4+"\"");
        System.out.println("str3=\""+str3+"\"");
    }

    public static void main(String[] args) {
        System.out.println();
        System.out.println("--------创建usingAttribute对象---------");
        System.out.println();
        //匿名对象
        new usingAttribute();
    }
}

operation result

--------before static---------
str1="string-1"
str2="null"
--------after static---------
str1="string-1"
str2="String-2"

--------创建usingAttribute对象---------

--------before constructor--------
str1="string-1"
str2="String-2"
str3="string-3"
str4="null"
--------after constructor--------
str1="string-1"
str2="String-2"
str3="string-3"
str4="string-4"

Statement and using objects

Statement object

类名 对象名 = new 类名();
创建属于某类的对象,需要通过下面两个步骤来实现
1、声明指向“由类所创建的对象”的变量
2、利用new创建新的对象,并指派给先前所创建的变量

For example: If you want to create an object of the Person class, can be used to achieve the following statement

//1、声明一个Person对象,再用new关键字实例化Personp1
Person p1;
p1 = new Person;

//2、声明Person对象p1并直接实例化此对象
Person p1 = new Person();

对象只有在实例化之后才能被使用,而实例化对象的关键字就是new

Use objects

If a member variable or method to access an object inside, can be achieved through the following syntax

对象名称.属性名     //访问属性
对象名称.方法名()   //访问方法

Use properties and methods of the class object invokes the Person class in

public class ObjectDemo {
    public static void main(String[] args) {
        //实例对象,调用构造方法
        Person p1 = new Person();
        //对p1的name和age进行赋值
        p1.name = "leesin";
        p1.age = 30;
        //调用talk方法
        p1.talk();
    }

    //Person类的定义
    static class Person{
        String name;
        int age;
        void talk(){
            System.out.println("我是:"+name+",今年:"+age+"岁");
        }
    }
}

Anonymous objects

匿名对象是指没有名字的对象。实际上,根据前面的分析,对于对象实例化的操作来讲,对象真正有用的部分在堆内存里面,而栈内存只是保存一个对象引用名称(严格来说是对象在堆内存的地址),所以所谓的匿名对象就是指,只开辟了堆内存空间,而没有栈内存指向的对象。

Create anonymous objects

public class NoNameObject {
    public void say(){
        System.out.println("面向大海,春暖花开!");
    }
    public static void main(String [] args){
        //匿名对象,没有被其他对象所引用
        new NoNameObject().say();  //面向大海,春暖花开!
    }
}

Anonymous object has two characteristics:

1、匿名对象没有被其他对象所引用,即没有栈内存指向
2、因为没有栈内存指向,所以只能使用一次,之后就无法找寻的垃圾对象,会被垃圾回收器收回

Comparison of

    有两种方式可用于对象间的比较:
        1、利用“==”运算符
            “==”运算符用于比较两个对象的内存地址值是否相等
        2、利用equals()方法
            equals()方法用于比较两个对象的内容是否相等

Examples of objects to compare

public class CompareObject {
    public static void main(String[] args) {
        String s1 = new String("java");
        String s2 = new String("java");
        String s3 = s2;

        if(s1 == s2){
            System.out.println("s1 == s2");
        }else {
            System.out.println("s1 != s2"); //s1 != s2
        }

        if (s2 == s3){
            System.out.println("s2 == s3"); //s2 == s3
        }else {
            System.out.println("s2 != s3");
        }

        System.out.println(s3.equals(s2)); //true
        System.out.println(s3.equals(s1)); //true
    }
}

Use an array of objects

把类理解为用户自定义的数据类型,它和基本数据类型(int、float等)具有等同的地位。。
通过两个步骤可以用数组来存放对象
    1、声明以类为数据类型的数组变量,并用new分配内存空间给数组
    2、用new产生新的对象,并分配内存空间给它

To create three Person type of array elements with the following syntax

Person p[]; //声明Person类类型的数组变量
p = new Person[3]; //用new分配内存空间

Once you've created an array element, the array elements can point to an object Person as defined class

p[0] = new Person();
p[1] = new Person();
p[2] = new Person();

Example: Create a Person class

public class Person {
    String name;
    int age;

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String talk(){
        return "我是" + this.name + ",今年:" + this.age + "岁";
    }
}

Example: Declaring an array of objects

public class ObjectArray {
    public static void main(String[] args) {****
        Person p[] = {
                new Person("leesin",30),
                new Person("zed",35),
                new Person("king",25)
        };

        for (int i = 0; i < p.length; i++) {
            System.out.println(p[i].talk());
        }
        /*
        我是leesin,今年:30岁
        我是zed,今年:35岁
        我是king,今年:25岁
         */
    }
}

Keyword

this keyword

1、this调用本类中的属性,也就是类中的成员变量;
2、this调用本类中的其他方法;
3、this调用本类中的其他构造方法,调用时要放在构造方法的首行

Use the Person class above to resolve this keyword

The static keyword

程序中如果用static定义属性的话,则此属性成为静态属性

Do not use the static properties of inconvenience

class Person{
    String name;
    int age;
    String nation;

    public Person(String name,int age,String nation){
        this.name = name;
        this.age = age;
        this.nation = nation;

    }

    public String talk(){
        return "我是:"+this.name+",今年:"+this.age+",来自:"+this.nation;
    }
}
public class noStaticDemo {
    public static void main(String[] args) {
        Person p1 = new Person("李青",30,"艾欧尼亚");
        Person p2 = new Person("艾瑞利亚",35,"艾欧尼亚");
        System.out.println(p1.talk());
        System.out.println(p2.talk());
    }

    /*output
        我是:李青,今年:30,来自:艾欧尼亚
        我是:艾瑞利亚,今年:35,来自:艾欧尼亚
     */
}
在程序中可以看到,两个Person对象都有一个nation属性,而且都相同。
如果要生产500个对象,每个对象的nation属性都相同,浪费空间不说,如果想修改所有人的nation属性,就要实施500次nation属性修改,这显然太麻烦。所以java提供了static关键字,用它来修饰类的属性后,此属性就是公共属性了。

With a static modification nation

class Person{
    String name;
    int age;
    static String nation;

    public Person(String name,int age,String nation){
        this.name = name;
        this.age = age;
        this.nation = nation;

    }

    public String talk(){
        return "我是:"+this.name+",今年:"+this.age+",来自:"+this.nation;
    }
}
public class noStaticDemo {
    public static void main(String[] args) {
        Person p1 = new Person("李青",30,"艾欧尼亚");
        Person p2 = new Person("艾瑞利亚",35,"艾欧尼亚");
        System.out.println("修改之前的信息:"+p1.talk());
        System.out.println("修改之前的信息:"+p2.talk());
        System.out.println("---------修改之后的信息----------");
        p1.nation = "均衡教派";
        System.out.println("修改之后的信息:"+p1.talk());
        System.out.println("修改之后的信息:"+p2.talk());
    }

    /*output
        修改之前的信息:我是:李青,今年:30,来自:艾欧尼亚
        修改之前的信息:我是:艾瑞利亚,今年:35,来自:艾欧尼亚
        ---------修改之后的信息----------
        修改之后的信息:我是:李青,今年:30,来自:均衡教派
        修改之后的信息:我是:艾瑞利亚,今年:35,来自:均衡教派
     */
}

final keyword

final关键字用于修饰不可改变的内容

final: immutable. It can be used to modify the class, method and variable

  • Categories: modified class can not be inherited
  • Methods: The modified method can not be overridden
  • Variables: modified variables can not be reassigned

    Modified class

final class 类名{
}

Modification methods

修饰符 final 返回值类型 方法名(参数列表){
    //方法体
}

Modified variables

1, local variables - basic types of
local variables of primitive types, was modified after the final, can only be assigned once, can not be changed

public class Demo01 {    
    public static void main(String[] args) { 
        //声明变量,使用final修饰        
        final int a;        
        //第一次赋值        
        a = 10;       
        //第二次赋值       
        //a = 20; //报错,不可重新赋值        
        
        //声明变量,直接赋值,使用final修饰        
        final int b = 10;        
        //第二次赋值        
        b = 20;//报错,不可重复赋值    
    }
}

2, local variables - reference type
local variable reference types, was modified after the final, can only point to an object, the address can not be changed, but does not affect the value of the member variables inside the object changes

public class Demo02 {
    public static void main(String[] args) {
        //创建user对象        
        final User u = new User();        
        //创建另一个User对象        
        //u = new User();  // 报错,指向了新对象,地址值改变        
        //调用setName方法        
        u.setName("leesin");    
    }
}

3, member variables
member variables to design initialization problems, there are two ways to initialize only a second election

  • Display initialization
public class User{
    final String USERNAME = "leesin"
    private int age;
}
  • Constructor to initialize
public class User{
    final String USERNAME;
    private int age;
    public User(String username, int age){
        this.USERNAME = username;
        this.age = age
    }
}

The modified final constant names, generally writing specifications, all capital letters

Guess you like

Origin www.cnblogs.com/youngleesin/p/12122111.html