1 / 10

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA. Object-Oriented Programming. มหาวิทยาลัยเนชั่น http:// www. nation. ac.th. บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 24 สิงหาคม 2550. โปรแกรมภาษาจาวา. class human { public static void main (String args[]) { System.out.println(“wow”); } }.

thuy
Télécharger la présentation

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. การโปรแกรมเชิงวัตถุด้วยภาษา JAVA Object-Oriented Programming มหาวิทยาลัยเนชั่น http://www.nation.ac.th บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 24 สิงหาคม 2550

  2. โปรแกรมภาษาจาวา class human { public static void main (String args[]) { System.out.println(“wow”); } }

  3. หลักพื้นฐานของ OO in Java 1. Inheritance 2. Encapsulation 3. Polymorphism 3.1 Overloading 3.2 Overriding

  4. Inheritance class father { void homeaddr(){System.out.println(“lp”);} } class child extends father { public static void main (String args[]) { homeaddr(); } }

  5. Encapsulation class friend { private int val; public int getter(){ return this.val;} public void setter(int a){ this.val=a;} } class child { public static void main (String args[]) { friend x = new friend(); x.setter(5); System.out.println(x.getter()); } }

  6. Polymorphism : Overloading overloaded methods: - appear in the same class or a subclass - have the same name but, - have different parameter lists, and, - can have different return type

  7. Polymorphism : Overloading class child { static int bag(){ return 1; } static int bag(int a){ return a; } static int bag(String a){ return a.length(); } public static void main (String args[]) { System.out.println(bag()); System.out.println(bag(5)); System.out.println(bag(“abc”)); } }

  8. Polymorphism : Overriding overriding methods: - appear in subclasses have the same name as a superclass method - have the same parameter list as a superclass method - have the same return type as as a superclass method - the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method if the superclass method is public, the overriding method must be public if the superclass method is protected, the overriding method may be protected or public if the superclass method is package, the overriding method may be packagage, protected, or public if the superclass methods is private, it is not inherited and overriding is not an issue - the throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including it's subclasses

  9. Polymorphism : Overriding (1/2) class father { static void addr(){System.out.println(“lp”);} } class child extends father { static void addr(){System.out.println(“cm”);} public static void main (String args[]) { addr(); new father().addr(); } }

  10. Polymorphism : Overriding (2/2) class father { void addr(){System.out.println(“lp”);} } class child extends father { void addr(){System.out.println(“cm”);} public static void main (String args[]) { child c = new child(); c.addr(); } }

More Related