Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes Lecture 7 from Chapter 6 2019/1/11.

Similar presentations


Presentation on theme: "Classes Lecture 7 from Chapter 6 2019/1/11."— Presentation transcript:

1 Classes Lecture 7 from Chapter 6 2019/1/11

2 Review Class definition Declaring Objects
2019/1/112019/1/11 Class definition Declaring Objects Assigning object reference variables Introducing methods Constructors The this keyword Finalized method

3 Introducing to Class The class is the core of Java.
2019/1/112019/1/11 The class is the core of Java. The class is the logical construct upon which the entire Java language is built. Class defines the shape and nature of an object. It is formed as the basics of object-oriented programming

4 The General Form of a class
2019/1/112019/1/11 Class classname { Type instance-variable1; // Type methodname1(parameter-list) { //body of method } Type methodname2 (parameter-list { //body of method } …. Class person { Double height; Double weight; }

5 Example - The General Form of a class
2019/1/112019/1/11 Class classname { Type instance-variable1; // Type methodname1(parameter-list) { //body of method } Type methodname2 (parameter-list { //body of method } …. class Pair { int x, y; Pair(int u, int v) { x = u; // y = v; } Pair(int x) { this.x = x; // y = 0; Pair() { x = 0;

6 Creating the object box
2019/1/112019/1/11 Person theperson = new person(); Here, theperson will be an instance of person. To assign a value, use theperson.weight = 70; theperson.height = 170;

7 Declaring the object – page 135
2019/1/112019/1/11 Statement Effect Person theperson; theperson = new Person(); Null theperson Weight theperson Height theperson.height

8 Example of creating and assigning value
2019/1/112019/1/11

9 Example of two objects, area and area1
2019/1/112019/1/11

10 Assigning object reference variables
2019/1/112019/1/11 Area a1 = new area(); Area a2 = a1; In this case, both a1 and a2 will refer to the same object Height a1 Width a2

11 Assigning object reference variables
2019/1/112019/1/11 Area a1 = new area(); Area a2 = a1; // a1 = null; In this case, a2 will point to object, but a1 will set to null. a1 null Height a2 Width

12 Introducing Methods Classes usually consist of two things:
2019/1/112019/1/11 Classes usually consist of two things: Instance variables (int x, y) and methods (pair())

13 Passing the values x = 1 and y = 2 will pass to Pair
2019/1/112019/1/11 x = 1 and y = 2 will pass to Pair

14 Returning a value – from myarea.area()
2019/1/112019/1/11

15 Example – with three methods
2019/1/112019/1/11

16 Constructor 2019/1/112019/1/11 It is tedious to initialise all variables in a class each time an instance is created. Constructors are a special sort of method that initialise freshly minted objects. They are like static methods in that we don't need an existing object to use them, just new.

17 Example 2019/1/112019/1/11 the values are fixed

18 Constructor In the previous example, the values are fixed
2019/1/112019/1/11 In the previous example, the values are fixed In order to create different values, we use a parameterised constructor.

19 Example of parameterised constructor
2019/1/112019/1/11

20 This Keyword This refers to the current object.
2019/1/112019/1/11 This refers to the current object. Or it is used instead of the class name as the constructor name. Example this.name = name;

21 Example of this 2019/1/112019/1/11

22 Finalise() 2019/1/112019/1/11 It is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. The purpose is to clean up an object before the object is garbage collected. Protected void finalise() { //finalise code }

23 More examples of class 2019/1/112019/1/11

24 More examples of class 2019/1/112019/1/11 import java.io.*;
class D2Class { String name; int birth; int num = 0; static int nextNum = 0; D2Class() num = nextNum; ; } D2Class(String s, int i) this(); name = s; birth = i; public void show() System.out.println("nextnum = " + D2Class.nextNum); System.out.println("num = " + this.num); System.out.println("Name = " + this.name); System.out.println("birth = " + this.birth); class a0502 public static void main(String args[]) throws IOException System.out.println("nextNum = " + D2Class.nextNum); D2Class d = new D2Class("Chan", ); D2Class d1 = new D2Class("Tai", ); d.show(); d1.show(); System.out.println("num = " + d1.num);

25 Summary Class definition – the core of java
2019/1/112019/1/11 Class definition – the core of java Declaring Objects – Person thisperson = new Person() Assigning object reference variables – point to object Introducing methods – class consists of variables and methods Constructors – initialise the variables The this keyword – this class Finalized method – perform some actions when it is destroyed


Download ppt "Classes Lecture 7 from Chapter 6 2019/1/11."

Similar presentations


Ads by Google