Object and classes
This presentation is the property of its rightful owner.
Sponsored Links
1 / 76

Object and classes PowerPoint PPT Presentation


  • 60 Views
  • Uploaded on
  • Presentation posted in: General

Object and classes. บทนี้เรียนอะไรบ้าง. แนะนำ Object-oriented programming บอกวิธีการสร้าง object จากคลาสที่จาวาให้มา สอนวิธีสร้างคลาสของเราเอง. แนะนำ Object-Oriented Programming. โปรแกรมประกอบขึ้นจากออบเจ็กต์ แต่ละออบเจ็กต์จะมีฟังก์ชั่นให้เรียกใช้

Download Presentation

Object and classes

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Object and classes

Object and classes


Object and classes

  • Object-oriented programming

  • object


Object oriented programming

Object-Oriented Programming


Object and classes

  • template

    • instance


Object and classes

    • instance fields instance variables

      • instance field

      • state

      • state

    • methods


Object and classes

  • instance field

    • instance field

      • a.x = 100; x 99


Extends

extends

  • extends

  • extends inheritance


Relationship between classes

Relationship between classes

  • Dependence (uses-a)

    • A depend on B A B

  • Aggregation (has-a)

    • A B aggregation

  • Inheritance (is-a)

    • Car extends Vehicle

    • Car specialise Vehicle

    • Car is a Vehicle Vehicle Car


Object and classes

  • UML Diagram

    • ArgoUML (http://argouml.tigris.org) Violet (http://violet.sourceforge.net)


Object and classes

    • ()


Object and classes

zoom

Date x = new Date();

initialize


Object and classes

  • ( toString() )

    • System.out.println(new Date());

    • String s = new Date().toString();

toString()


Object and classes

  • Date x

    • s = x.toString(); error

x Date

x


Object and classes

  • x

    • : x = new Date();

    • : x = birthday;


Object and classes

  • Date x = new Date();

  • Date x2 = x;

x

pointer

Date

x

Date object x x2

X2

Date


Object and classes

    • X = null;

  • null

    • if (x != null){ }

  • null error


Object and classes

  • Date

    • Date Date

      if(today.before(birthday))

      System.out.println(Have some time left.);


Object and classes

  • GregorianCalendar

    • Extends Calendar

    • new GregorianCalendar()

      new GregorianCalendar(1999, 11, 31) 31 1999 ( 0)


Mutator and accessor methods

Mutator and accessor methods

  • Date

  • accessor methods ( )

    // construct d as current date

    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);

    int month = d.get(Calendar.MONTH);


Object and classes

  • mutator methods ()

    d.set(Calendar.DAY_OF_MONTH, 15);

    d.set(Calendar.YEAR, 2001);

    d.set(Calendar.MONTH, Calendar.APRIL);

    d.add(Calendar.MONTH, 3); d


Object and classes

  • accessor get mutator set

    Date time = calendar.getTime();

    calendar.setTime(time);

( Date) time

Calendar

calendar object


Object and classes

  • Date

    • Date calendar

      GregorianCalendar cal = new GregorianCalendar(year, month, day);

      Date hireDay = calendar.getTime();

  • Date object Date object

    GregorianCalendar cal = new GregorianCalendar();

    Cal.setTime(hireDay);

    int year = cal.get(Calendar.YEAR);


Object and classes

  • calendar


Object and classes

  • main

  • field

  • main method


Object and classes

class Employee{

//constructor

public Employee(String n, double s, int year, int month, int day){

name = n;

salary = s;

GregorianCalendar calendar = new GregorianCalendar(year, month-1, day);

hireDay = calendar.getTime();

}

//method

public String getName(){

return name;

}

//instance fields

private String name;

private double salary;

private Date hireDay;


Employee

Employee

  • EmployeeTest

  • 5%

    • main EmployeeTest

    • public

    • public class

    • .class

    • main


Object and classes

  • javac Employee*.java

    Javac EmployeeTest.java

    EmployeeTest Employee Employee.class Employee.java

    .class .java


Employee1

Employee

    • public

    • private Employee

      • public

    • name String hireDay Date


Constructors

constructors

public Employee(String n, double s, int year, int month, int day)

{

name = n;

salary = s;

GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);

// GregorianCalendar uses 0 for January

hireDay = calendar.getTime();

}


Object and classes

new Employee(James Bond, 100000, 1950, 1, 1); object Employee

new

name = James Bond

Salary = 100000;

hireDay = January 1, 1950


Object and classes

  • local variable field

    public Employee(String n, double s, int year, int month, int day)

    {

    String name = n;

    double salary = s;

local variable fields


Implicit and explicit parameters

Implicit and explicit parameters

public void raiseSalary(double byPercent)

{

double raise = salary * byPercent / 100;

salary += raise;

}

Employee number007 = new Employee(James Bond, 100000, 1950, 1, 1);

number007.raiseSalary(5);

Implicit parameter

Explicit parameter


Object and classes

  • double raise = number007.salary * byPercent / 100;

    number007.salary += raise;

    implicit parameter this

    double raise = this.salary * byPercent / 100;

    this.salary += raise;


Employee2

Employee

accessor

public String getName()

{

return name;

}

public double getSalary()

{

return salary;

}

public Date getHireDay()

{

return hireDay;

}

  • name public name private

  • salary private raiseSalary raiseSalary public


Object and classes

Employee

String firstName;

String lastName;

Employee getname return firstName + + lastName;

error salary 0 salary salary 0


Object and classes

public Date getHireDay()

{

return hireDay;

}

Date

Date d = harry.getHireDay();

d.setTime();

d hireDay harry setHireDay() Employee


Object and classes

    • public Date getHireDay()

      {

      return (Date) hireDay.clone();

      }

pointer


Access privileges

Access privileges

  • private data

  • private data

    class Employee{

    boolean equals(Employee other){

    return name.equals(other.name);

    }

java


Private methods

Private methods

  • public

  • private method

    • public


Final instance fields

Final instance fields

  • primitive type immutable class ( immutable class String )

  • private final Date hireDate; pointer hireDate


Static fields and methods

Static fields and methods

  • a static

    • ( )

    • a ( a )

      class Employee{

      private int id;

      private static intnextId =1;

      public void setID(){

      id = nextId;

      nextId++;

      }


Object and classes

Employee harry

harry.setId();

harry.id = Employee.nextId;

Employee.nextId++;

class

object


Static constants

Static constants

  • Math

    public static final double PI = 3.141592653589.

    double x = Math.PI;

  • static PI instance field Math

    • PI

    • PI ()


Object and classes

  • static constant System.out

    public class System{

    public static final PrintStream out = ;

    }

public final


Object and classes

  • System setOut out native method


Static methods

Static methods

  • Math.pow(x, a);

  • static method instance field

  • static method


Object and classes

    • static field

    • Main method


Method parameters

Method parameters

  • Java call by value

    • add(a,b) -> a b a b

      public static void tripleValue(double x){

      x = 3x;

      }


Object and classes

double percent =10;

tripleValue(percent);

percent 10

x


Object and classes

  • primitive object reference

  • public static void tripleValue(Employee m){

    m.raiseSalary(200);

    }

    Employee harry = new Employee();

    tripleValue(harry);

    copy pointer pointer

    salary m harry


Object and classes

Employee

harry

m

raiseSalary


Object and classes

  • public static void swap(Employee x, Employee y)

    {

    Employee temp = x;

    x = y;

    y = temp;

    }

    Employee a = new Employee(Alice, );

    Employee b = new Employee(Bob, );

    swap(a,b);

pointer a b a b


Object and classes

Alice

a

Bob

b

x

y


Object and classes

Alice

a

Bob

b

x

temp

a b x y

y


Object and classes

  • primitive type

  • pointer

  • ParamTest.java


Object construction

Object construction

new GregorianCalendar()

new GregorianCalendar(1999, 11, 31)

Overloading:

compile error

Overloading


Object and classes

  • + signature

  • Return type

  • default

    • 0

    • boolean false

    • Object reference null

    • field local variable

    • null error


Object and classes

  • hireDay Employee null

  • Calendar.setTime(harry.getHireDay());

    • throw exception setTime null


Default constructor

Default constructor

  • default

    • instance field default

  • default constructor default constructor default constructor


Object and classes

  • assign :

    class Employee{

    static intassignID(){

    int r = nextId;

    nextId++;

    return r;

    private int id = assignId();

    }

initialize initialize


Object and classes

public Employee(String n, double s){

name = n;

salary = s;

}


Object and classes

public Employee(String aName, double aSalary){

name = aName;

salary = aSalary;

}


Object and classes

public Employee(String name, double salary){

this.name = name;

this.salary = salary;

}

instance field


Object and classes

public Employee(double s){

this(Employee # + nextId, s);

nextId++;

}


Initialization block

Employee

Initialization block

private int id;

private String name = ""; // instance field initialization

private double salary;

// static initialization block

static

{

Random generator = new Random();

// set nextId to a random number between 0 and 9999

nextId = generator.nextInt(10000);

}

// object initialization block

{

id = nextId;

nextId++;

}

static initialize

initialize static variable initialize

Execute


Object and classes

    • default

    • Field initializer initialization block (static initialization )

  • ConstructorTest.java


Packages

packages

  • package

    • java.lang

    • java.util

    • java.net

  • java javax


Object and classes

  • package

    • Employee package

    • package

      • sun com.vishnu

      • subpackage com.vishnu.progmeth

  • java.util java.util.jar


Import

import

  • public

    • Java.util.Date today = new java.util.Date();

    • import

      • import package package

      • import java.util.*;


Object and classes

  • import java.*;

    import java.*.*;

  • java.util.* java.sql.* Date

    • Date

    • import java.util.Date;

    • Date


Static import

Static import

import static java.lang.System.*;

static static field System

out.println();

Math constant


Object and classes

  • source file

    package com.vishnu.progmeth;

    public class Employee{

    }

    default package


Object and classes

  • com.vishnu.progmeth com\vishnu\progmeth base directory

  • .class source file .class


Package scope

Package scope

  • public private

  • seal package

    • seal jar ( eclipse )


Class path

Class path

  • Jar file zip subdirectory

  • classpath

    c:\classdir;.;c:\archives\archive.jar

*

Jar file

Base directory

Current directory


Object and classes

  • library

  • class path

  • import java.util.*;

    import com.vishnu.progmeth.*;

    Employee

    Java.lang.Employee, java.util.Employee, com.vishnu.progmeth.Employee Employee current package class path


  • Login