JAVA Programming 1
(inheritance) 2!,!!
4 3
4!!!!
5 public class Person {... public class Student extends Person { // Person Student... public class StudentWorker extends Student { // Student StudentWorker...! -> (super class)! -> (sub class)! extends
5-1 : - Point ColorPoint 6 (x, y) Point ColorPoint. class Point { private int x, y; // x, y public void set(int x, int y) { this.x = x; this.y = y; public void showpoint() { // System.out.println("(" + x + "," + y + ")"); // Point ColorPoint class ColorPoint extends Point { private String color; // public void setcolor(string color) { this.color = color; public void showcolorpoint() { // System.out.print(color); showpoint(); // Point showpoint() public class ColorPointEx { public static void main(string [] args) { Point p = new Point(); // Point p.set(1, 2); // Point set() p.showpoint(); (1,2) red(3,4) ColorPoint cp = new ColorPoint(); // ColorPoint cp.set(3, 4); // Point set() cp.setcolor("red"); // ColorPoint setcolor() cp.showcolorpoint(); //
5-1 7 * new ColorPoint()
8
9 java.lang.object java.lang.object
10 4! public, protected,, private private protected private! private!! public! public protected!! protected
11
5-2: 12 Person Student Person. Person private weight Student Person getxxx, setxxx. int age; public String name; protected int height; private int weight; class Person { private int weight; int age; protected int height; public String name; public void setweight(int weight) { this.weight = weight; public int getweight() { return weight; class Student extends Person { public void set() { age = 30; // name = ""; // public height = 175; // protected // weight = 99; //. private setweight(99); // private weight setweight() public class InheritanceEx { public static void main(string[] args) { Student s = new Student(); s.set();
/ 13 new!!,!
14
15!!!! super()
16
17 Implicit super constructor A() is undefined. Must explicitly invoke another constructor
18
super() 19 super()! super(parameter);
20 super()
5-3 : super() ColorPoint 21 super() ColorPoint Point. x=5, y=6 class Point { private int x, y; // x, y public Point() { this.x = this.y = 0; public Point(int x, int y) { this.x = x; this.y = y; public void showpoint() { // System.out.println("(" + x + "," + y + ")"); class ColorPoint extends Point { private String color; // public ColorPoint(int x, int y, String color) { super(x, y); // Point Point(x, y) this.color = color; public void showcolorpoint() { // System.out.print(color); showpoint(); // Point showpoint() public class SuperEx { public static void main(string[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showcolorpoint(); x=5, y=6, color = "blue" blue(5,6)
. 22.,.
(upcasting) 23!!?! class Person { class Student extends Person { Student s = new Student(); Person p = s; //,!
24
(downcasting) 25?!! class Person { class Student extends Person {... Person p = new Student(""); // Student s = (Student)p; //, (Student)
26
instanceof 27! ) '' (), instanceof!! instanceof : true/false
? 28 1. 2. Person p = new Person(); Person p = new Student(); // Person p = new Researcher(); // Person p = new Professor(); // 3. print(p);, print() person void print(person person) { // person Person, // Student, Researcher, Professor....
29 Person person
30 instanceof
5-4 : instanceof 31 instanceof.? class Person { class Student extends Person { class Researcher extends Person { class Professor extends Researcher { public class InstanceOfEx { static void print(person p) { if(p instanceof Person) System.out.print("Person "); if(p instanceof Student) System.out.print("Student "); if(p instanceof Researcher) System.out.print("Researcher "); if(p instanceof Professor) System.out.print("Professor "); System.out.println(); public static void main(string[] args) { System.out.print("new Student() ->\t"); print(new Student()); System.out.print("new Researcher() ->\t"); print(new Researcher()); System.out.print("new Professor() ->\t"); print(new Professor()); new Professor() Person, Researcher, Professor new Student() -> Person Student new Researcher() -> Person Researcher new Professor() -> Person Researcher Professor
32 (Method Overriding)!,,!,!
33 Shape draw() Line, Rect, Circle
34
, 35!,! ' ( ) ' Line draw() Circle draw() Rect draw()!
5-5 : 36 class Shape { // public Shape next; public Shape() { next = null; public void draw() { System.out.println("Shape"); class Line extends Shape { public void draw() { // System.out.println("Line"); class Rect extends Shape { public void draw() { // System.out.println("Rect"); class Circle extends Shape { public void draw() { // System.out.println("Circle"); public class MethodOverridingEx { static void paint(shape p) { p.draw(); // p draw(). // public static void main(string[] args) { Line line = new Line(); paint(line); paint(new Shape()); paint(new Line()); paint(new Rect()); paint(new Circle()); Line Shape Line Rect Circle
37
// 5-5 Shape, Line, Rect, Circle public class UsingOverride { public static void main(string [] args) { Shape start, last, obj; // start = new Line(); // Line last = start; obj = new Rect(); last.next = obj; // Rect last = obj; obj = new Line(); // Line last.next = obj; last = obj; obj = new Circle(); // Circle last.next = obj; // Shape p = start; while(p!= null) { p.draw(); p = p.next; Line Rect Line Circle 38
(run time) 39
super 40 super super
5-6 : 41 41 Weapon fire(). fire() 1. class Weapon { protected int fire() { return 1; // Weapon Cannon. Cannon 10. fire(). main(). class Cannon extends Weapon { @Override protected int fire() { // return 10; // 10 public class Overriding { public static void main(string[] args) { Weapon weapon; weapon = new Weapon(); System.out.println(" " + weapon.fire()); weapon = new Cannon(); System.out.println(" " + weapon.fire()); 1 10
42 vs.
43 (abstract method)!, abstract public abstract String getname(); public abstract void setname(string s);! (abstract class) 2 1. abstract 2. abstract
2 44 // 1. abstract class Shape { // public Shape() { public void paint() { draw(); abstract public void draw(); // // 2. abstract class MyComponent { // String name; public void load(string name) { this.name = name;
45 abstrct class Shape { public class AbstractError { public static void main(string [] args) { Shape shape; shape = new Shape(); //. Shape....
46 2 1., abstract abstract class Shape { // public Shape() { public void paint() { draw(); abstract public void draw(); // abstract class Line extends Shape { //. draw() public String tostring() { return "Line"; 2. ()
47 drow(). draw()
48!!
5-7 : 49 Calculator GoodCalc. abstract class Calculator { public abstract int add(int a, int b); public abstract int subtract(int a, int b); public abstract double average(int[] a);
5-7 50 public class GoodCalc extends Calculator { @Override public int add(int a, int b) { // return a + b; @Override public int subtract(int a, int b) { // return a - b; @Override public double average(int[] a) { // double sum = 0; for (int i = 0; i <a.length; i++) sum += a[i]; return sum/a.length; public static void main(string [] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2,3,4 )); 5-1 3.0
51 (). ()
52!! ( )! Interface! Ex) public interface SerialDriver {! : public abstract.! : public static final.!
53
54 abstract public public static final
55! extends interface MobilePhoneInterface extends PhoneInterface { void sendsms(); // void receivesms(); //! interface MusicPhoneInterface extends MobilePhoneInterface, MP3Interface { void playmp3ringtone(); //
56! implements!! class SamsungPhone implements MobilePhoneInterface { // // MobilePhoneInterface. public void sendcall() {... public void receivecall() {... public void sendsms() {... public void receivesms() {... //. public int getbuttons() {...
57, MobilePhoneInterface sendcall() sendcall() sendcall() sendcall() sendcall() IPhone SamsungPhone LGPhone
58 interface AIInterface { void recognizespeech(); // void synthesizespeech(); // class AIPhone implements MobilePhoneInterface, AIInterface { // // MobilePhoneInterface. public void sendcall() {... public void receivecall() {... public void sendsms() {... public void receivesms() {... // AIInterface. public void recognizespeech() {... // public void synthesizespeech() {... // //. public int touch() {...
5-8 : 59 interface PhoneInterface { int BUTTONS = 20; // void sendcall(); void receivecall(); interface MobilePhoneInterface extends PhoneInterface { void sendsms(); void receivesms(); interface MP3Interface { // public void play(); public void stop(); class PDA { public int calculate(int x, int y) { return x + y; class SmartPhone extends PDA implements MobilePhoneInterface, MP3Interface { @Override public void sendcall() { System.out.println(" "); @Override public void receivecall() { System.out.println(" "); @Override public void sendsms() { System.out.println("SMS "); @Override public void receivesms() { System.out.println("SMS "); // MP3Interface @Override public void play() { System.out.println(" "); @Override public void stop() { System.out.println(" "); // @Override public void schedule() { System.out.println(" "); public class InterfaceEx { public static void main(string [] args) { SmartPhone p = new SmartPhone(); p.sendcall(); p.play(); System.out.println(p.calculate(3,5)); p.schedule(); 8
60!,!
61