1. Trang chủ
  2. » Giáo án - Bài giảng

Giáo trình Java cơ bản 18

54 302 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 54
Dung lượng 708,57 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

18/19 Defining the class Subject go through the following typical steps – Sketch a model of the class – Define the class header – Define the attributes – Define the constructors – Defi

Trang 2

18/2

Lecture overview

– Define classes

– Create objects

– Use (manipulate) objects

Trang 3

18/3

► The concepts of object and

class

Trang 5

18/5

Objects

general

an object is that it can be distinguished from

other things

Trang 6

18/6

Objects

– Tangible things (e.g tables, books, toasters)

– People (e.g employees, students)

– Organisations (e.g universities, companies,

departments)

– Non-tangible things (e.g accounts, events)

– etc

Trang 7

18/7

Objects

concept of object is more or less preserved

– Things maintained in Java programs are either

primitive data values or objects

– We can define classes to represent real-life

objects

Trang 8

18/8

in general

(represented by its attributes or fields) and a set of behaviours (represented

by its methods)

Object - a definition

Trang 9

18/9

Classes

grouped and considered together

describe and generate objects

Trang 10

18/10

Elements of a Java class

Trang 11

18/11

Abstractions

programming) are abstractions

– Simplified versions / representations of the real

things

– Only features relevant (to some purpose) are

represented

Trang 12

18/12

► How the concepts are put

to work

Trang 13

18/13

How the concepts are put to work

following sequence of activities

– Defining classes

– Creating objects

– Manipulating objects

Trang 14

18/14

Defining classes

precede that of class

a programmer

define the classes first

Trang 16

18/16

Creating objects

objects (instances)

the class) with the new operator which

utilises a constructor of the class

Trang 17

18/17

Manipulating objects

them by sending messages to them

perform certain methods

“associated with it” (i.e defined in its class

or one of the super classes of the class)

Trang 18

18/18

Example

 Create and test a class representing a subject, that

– Has a name, a lecturer, a quota and a current enrolment

size

– Allows users to enrol a student in the subject, un-enrol

them from the subject, or display the details of the

subject

 Rules

– Cannot enrol a student when quota has been reached

– Cannot un-enrol a student when there are no students in

the class

Trang 19

18/19

Defining the class Subject

go through the following typical steps

– Sketch a model of the class

– Define the class header

– Define the attributes

– Define the constructors

– Define the methods

Trang 21

18/21

Specifying the class header

public class Subject

{

}

Trang 22

18/22

Declaring the attributes

public class Subject {

private String name;

private String lecturer;

private int quota;

private int currentEnrolment;

}

declared to be private (more later)

Trang 23

18/23

Defining the constructors

public Subject(String subjectName,

String lecturerName, int maxQuota)

Trang 24

18/24

Constructor

 When a class is instantiated, a special initialisation

method called a constructor is automatically

invoked

 The constructor is defined as a method within the

class definition

 It has the same name as the class itself

 In the example, it sets the initial values of the

attributes to the values specified by the user

creating the instance

Subject s = new Subject("Chemistry", "Dr Jekyll", 20);

Trang 26

18/26

Method to enrol a student

public void enrolStudent( )

Trang 27

18/27

Method to un-enrol a student

public void unEnrolStudent( )

Trang 28

18/28

Method to display subject info

public void displaySubjectInfo( )

int availablePlaces = quota – currentEnrolment;

System.out.println("Can accept " + availablePlaces +

" more students");

}

Trang 29

18/29

Local variables

 availablePlaces is defined in the

displaySubjectInfo method and is only available to

that method

 It is created when the displaySubjectInfo method

is invoked, and destroyed when that method ends

 Its scope is the method displaySubjectInfo

 It is not an attribute of the Subject class or its

instances, nor is it available for use in any other

method defined by Subject

Trang 30

18/30

► Creating and manipulating

objects

Trang 31

18/31

Testing the Subject class

Subject

– This class has a main( ) method

– In the main method, we create instances of

Subject and send messages to them

Trang 33

18/33

Invoking a method

 To invoke a method in an object, we send it a

message

 In Java, we specify the name of the object to

receive the message and specify which of that

object’s methods is to be invoked by using the dot

operator

 The receiver object is on the left hand side (lhs) of

the dot, the method’s name is on the right hand

side (rhs) of the dot

physics.displaySubjectInfo( );

Trang 35

18/35

Example

// Test 3 - Make an invalid un-enrol request and see how it // is handled (i.e un-enrol the current one student // and then try to un-enrol another student)

Trang 37

18/37

► Summary

Trang 39

18/39

public class Dog

{

private String name;

private String breed;

private int age;

private boolean isPedigreeDog;

Java class summary

Attributes that describe the state of a dog

Methods that describe the behaviour of a dog

Constructor to initialise Dog objects

Trang 40

18/40

► Further example

Trang 41

18/41

Class exercise

– Has a radius

– Allows the user to

 display the radius

 calculate and display the diameter,

 calculate and display the perimeter,

 calculate and display the area,

 and display all the measurements at once

Trang 42

displayAll()

Sketching a model for the class

Trang 43

18/43

Specify the class header

Trang 44

18/44

Declare the attributes

Trang 45

18/45

Define a constructor

Trang 46

18/46

Define the methods

 to display the radius

 to calculate and display the diameter

 to calculate and display the perimeter

 to calculate and display the area

 to display all details

Trang 47

18/47

Method to display the radius

Trang 48

18/48

Method to display the

diameter

Trang 49

18/49

Method to display the

perimeter

Trang 50

18/50

Method to display the area

Trang 51

18/51

Method to display all the

details

Trang 52

18/52

Using methods from the same

object

invoke a method in itself (the object), it

must send a message to itself

object but can simply give the name of the

method to be called

Trang 53

18/53

Java files and separate

compilation

 Each Java class needs to be in a separate file which

has the same name as the class but with a java

extension

 A class can be compiled to byte code before a

program exists to use the class

 When you create a launcher class that uses this class, you do not need to recompile the class

 If all the classes in your program are located in the

same directory, the Java interpreter finds the right

class when it is needed

 Later we will look at using classes in other directories

Ngày đăng: 24/03/2016, 22:14

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN