Getting started in the Java language Tutorial (10): Correlation and dependencies in the Java language

Source: Internet
Author: User
Tags modifiers

Before beginning the study of this article, first summarize the content that has already studied. In the previous 9 articles, the main introduction of Java classes in the basic syntax, is based on the first summary, and then the specific step-by-step approach. First, we introduce the main components of Java classes: data members, method members, and construction methods. Then, some general knowledge points are introduced in detail, such as permission access modifiers, packages, static modifiers, data types and so on. After understanding these common knowledge points, the three components of the class are introduced in detail, such as the related problems of various data in the class, the related problems of the construction method are introduced, the operators that the method will use, the process control, and the value transfer which the method is used to call are introduced. After learning this, beginners should be able to understand a separate Java class more clearly. However, in Java applications, it is not possible to have only one Java class, but there are many Java classes. These many Java classes can not be ways, but will collaborate with each other to complete complex functions. So, after understanding the basics, next, beginners should focus on learning the relationship between classes and classes in the Java language.

In this article, you will use a simple Java class to show two common relationships between classes and classes in the Java language: Associated relationships, and dependencies

1. Related relationship

Class A association Class B means that if an object of Class A is instantiated, there will be an object of Class B being instantiated. In short, b exists as an attribute of a. As follows:

Class a{
       private b b;
}
Class b{
}

2. Dependency relationship

Class A relies on class B, which means that if a object is to complete an operation, it must use some of the actions of the object of B to help it to complete. In short, b exists as a method parameter for a method of a. As follows:

Class a{public
       void F (b. b) {
}
}
class b{
}

Association and dependency are two of the most common relationships in object-oriented programming. Suppose there is a scenario description: A training center offers free courses, each subject to its name and original price. Students can choose a course at most, and students can choose a course to study as long as they register their names.

By analyzing this simple scenario, it is easy to analyze that there are two kinds of objects: students, courses. In other words, we should create two classes, Student,course.

The course class has two properties, course name and original price. As follows:

Package com.csst.relation;
public class Course {
       private String title;
       private double price;
       Public Course (String title, double price) {
              super ();
              this.title = title;
              This.price = Price;
       }
       Public Course (String title) {
              super ();
              this.title = title;
       }
       Public Course () {
              super ();
       }
       Public String GetTitle () {return
              title;
       }
       public void Settitle (String title) {
              this.title = title;
       }
       Public double GetPrice () {return price
              ;
       }
       public void Setprice (double price) {
              This.price = Price;
       }
}

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.