Java Personal Address Book

Table of contents

1.1 Personal communication rate (1)

1.2 Program Design Ideas

1.3 Key technologies

1.3.1 Basic concept of object-oriented

1.3.2 Class definition

1.4 Personal communication rate (1) Design steps

1.4.1 Define the class Contract

1.4.2 Define the class Family

1.4.3 Define the class Partner


1.1 Personal address book (1)

        Design a simple personal address book system (1), which is used to store contact information and realize the classification and storage of contacts according to basic contacts, family members, work partners, etc. The specific requirements are as follows: (1) The basic information of contacts
        includes names , gender, email and several contact numbers;
        (2) Family members need to define home address and birthday in addition to basic contact information;
        (3) Work partners need to add address in addition to basic contact information Company and job title/job information.


1.2 Program Design Ideas

        Entity objects can be regarded as components with internal properties and functions, and the implementation details of internal information and functions of entities are hidden through encapsulation technology. The personal address book (1) involves 3 basic categories, which are contact category, family category and work partner category.
        The contact class encapsulates the basic information and output functions of the contact; the family class reuses the contact class and adds unique attributes to describe the home address and birthday of the contact; the work partner class also reuses the contact class, and at the same time describes the location of the work partner through combination technology Company and job title/job information.


1.3 Key technologies

1.3.1 Basic concept of object-oriented

Java is an object-oriented programming language. Its basic idea is to program through basic concepts such as objects, classes, encapsulation, inheritance, and polymorphism. It builds software systems from objective things (that is, objects) that exist in the real world. .

Object: As the basic unit of the system, an object is a package composed of data and its behavior. An object contains three basic elements, which are object identification, object state, and object behavior. Each object must have a name to distinguish it from other objects, which is the object identifier; state is used to describe certain characteristics of the object; object behavior is used to encapsulate the business operations owned by the object.

For example, a contact Contract object contains basic status information such as name, gender, email, and contact number, and also has the behavior characteristics of printout.

Class: abstract the same kind of objects to form a class, which provides a unified abstract description for all objects belonging to the class, and its interior includes two main parts: state and behavior. Classes can also be considered as a custom data type, and variables can be defined using classes. All variables defined through classes are reference variables, and they will refer to instances of the class, that is, objects.

Encapsulation: Encapsulation is an information concealment technology. Through encapsulation, the state and behavior of an object are combined into an independent module, and the internal details of the object are hidden as much as possible (including the private state inside the object and the specific implementation of the behavior). The purpose of encapsulation is to separate the designer of the object from the user. As a user, he does not need to understand the internal implementation details of the object, but only needs to use the behavior method provided by the designer to realize the function.

Inheritance: Inheritance represents the hierarchical relationship between classes. The inheritance relationship enables subclass objects to share the state and behavior of parent class objects. Inheritance can be divided into single inheritance (a subclass has only one parent class) and multiple inheritance (a subclass can have multiple parent classes). The Java language supports single inheritance of classes, while C++ allows multiple inheritance. In the process of programming, through inheritance, on the one hand, the hierarchical structure of classes is obtained; on the other hand, through the inheritance relationship of classes, common state and behavior characteristics can be shared, which improves the reusability of software

Polymorphism: Polymorphism means that behavior methods with the same name can have different implementations in different classes. While the subclass inherits the parent class, the method implementation of the class can be expanded or modified, so that the method with the same name of the subclass is more suitable for the subclass object. For example, the drawing method draw() of the parent class Shape (Shape) has the drawing method draw() of the same name in its subclasses Circle (Circle) and Square (Square), but the content and method of drawing are different.

1.3.2 Class definition

A class is a custom reference data type, which can be defined in the following format:

[public|abstract|final] class 类名{
    [初始化块的定义;]
    [成员变量的定义;]
    [方法的定义;]
    
}

Public, abstract, and final are called modifiers, which are used to define attributes such as access limits, abstract classes, and final classes of classes. Square brackets ([ ]) indicate optional items. Class names must be valid Java identifiers. In order to improve the readability of the program, Java class names are usually composed of several meaningful words, the first letter of each word is capitalized, and the rest of the letters are all lowercase.
The content between the braces ({ }) is called the class body, which mainly includes 3 parts: the initial block, the definition of member variables, and the definition of methods. The definition order of each member in the class has no effect, and each member can call each other. The initialization block is used to initialize the class object; the member is used to define the state data of the same object; the method defines the behavior characteristics or function realization of the class.

For knowledge about classes and objects and member methods, please refer to the following articles:

Java Study Notes (11): Class and Object Variables/fields: Basic introduction: Case analysis: Notes and details: How to create objects? How to access properties? The memory allocation mechanism of basic grammar classes and objects: Java memory structure analysis The process of creating objects in Java Simple analysis concepts: Object: An object is an instance of a class, with state and behavior. For example, a dog is an object, its states are: color, name, breed; behaviors are: wagging tail, barking, eating, etc. Class: A class is a template that describes the behavior and state of a class of objects. Raise requirements: https://blog.csdn.net/long_0901/article/details/120170776?spm=1001.2014.3001.5501

Java Study Notes (12): Member Methods_She and the Sword Intent Lost Blog-CSDN Blog_How to Write Basic Introduction to Java Member Methods In some cases, we need to define member methods (methods for short). For example, human beings: In addition to some attributes (age, name..), we humans also have some behaviors such as: we can talk, run..., through learning, we can also do arithmetic problems. At this time, it is necessary to use member methods to complete. Now it is required to complete the Person class. Definition of member method public (access modifier) ​​return data type method name (parameter list..) {//method body statement; return return value;} Explanation: (1) formal parameter list: indicates member method input eg: person (int n) , getSum(int... https://blog.csdn.net/long_0901/article/details/124282927?spm=1001.2014.3001.5501


1.4 Personal Address Book (1) Design Steps

1.4.1 Define the class Contract

Complete the definition of the contract of the class:

(1) Contains instance variables such as name, gender, email, and several phone numbers.

(2) Generate a getter method and a setter method for each instance variable, where the name cannot be empty.

(3) Define three construction methods for the contact: the default construction method, the construction method containing all member variables, and the construction method containing the name and contact number.

(4) Define the display() method to output detailed information of member variables.

import static java.lang.Character.getName;

public class Contract {
    private String name;
    private String gender;
    private String email;
    private String[] phones;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(name==null||name.equals(""))//姓名不能为空
            return;
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String[] getPhones() {
        return phones;
    }

    public void setPhones(String[] phones) {
        this.phones = phones;
    }

    public Contract(String name, String gender, String email, String[] phones) {
        super();
        setName(name);
        setGender(gender);
        setEmail(email);
        setPhones(phones);
    }
    public Contract() {
    }
    public Contract(String name,String[] phones) {
        this(name,"","",phones);
    }
    public void display() {
        System.out.println("姓名:" + getName() + "\t性别:" + getGender() + "\temail:" + getEmail());
        System.out.print("联系电话:");
        for (int i = 0; i < phones.length; i++) {
            System.out.print(phones[i] + "\t");
        }
        System.out.println();
    }
    public static void main(String[] args){
        Contract c = new Contract("卢海龙",new String[]{"666666","888888","0376-10010"});
        c.display();
    }
}

1.4.2 Define the class Family

Define the family contact class Family according to the inheritance relationship:

(1) In addition to the real-time variables inherited from the parent class Contract, the class Family also needs to define two member variables of the family address (address) and birthday (birthday).

(2) Generate getter and seter methods for each member variable.

(3) Define two construction methods for the class Family, and realize the initialization of the object by calling the construction method of the parent class.

(4) Rewrite the display() method of the parent class to realize the output of the subclass object.

package chap05;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Family extends Contract{
    private Date birthday;
    private String address;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Family(String name, String gender, String email, String[] phones, Date birthday, String address) {
        super(name, gender, email, phones);
        setAddress(address);
        setBirthday(birthday);
    }

    public Family() {
        super();
    }

    public Family(String name, String[] phones) {
        super(name, phones);
    }
    public void display(){
        super.display();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("出生日期:"+sdf.format(getBirthday())+"\t家庭地址:"+getAddress());
    }
    public static void main(String[] args){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Family f = new Family("卢海龙","男","[email protected]",new String[]{"123456","888888"},sdf.parse("2002-09-01"),"河南省信阳市");
            f.display();
        }catch (ParseException e){
            e.printStackTrace();
        }
    }
}

1.4.3 Define the class Partner

Define the working partner class Partner and its company class Company.

(1) Realize the definition of the company class Companv.

(2) Define the partner class Partner through inheritance, and add its company (company) and title/title (title) member variables.

(3) Generate getters and setters for each member variable.

(4) Define two construction methods for the class Partner, and realize the initialization of the object by calling the construction method of the parent class.

(5) Rewrite the display() method of the parent class to realize the output of the subclass object.

package chap05;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Partner extends Contract{
    private String title;
    private Company company;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Partner(String name, String gender, String email, String[] phones, String title, Company company) {
        super(name, gender, email, phones);
       setTitle(title);
       setCompany(company);
    }

    public Partner() {
        super();
    }

    public Partner(String name, String[] phones) {
        super(name, phones);
    }
    public void display(){
        super.display();
        System.out.println("职务:"+getTitle()+"\n工作单位:"+getCompany());
    }
    public static void main(String[] args){
       Partner p = new Partner("卢海龙","男","[email protected]",new String[]{"666666","888888"},
               "Java工程师",new Company("阿里巴巴","上海浦东新区","0376-10001","0376-10002"));
       p.display();
    }
}

Guess you like

Origin blog.csdn.net/long_0901/article/details/124874736