![]() | ตัวอย่างโปรแกรม แสดง การโปรแกรมเชิงวัตถุ | ![]() |
class x {
public static void main(String args[]) {
System.out.println("x");
}
}
class x {
public static void main(String args[]) {
int i = 1;
System.out.println(i);
ok();
}
static void ok() {
System.out.println("xx");
}
}
// Result of this program
// 1
// xx
// Testa.java
// Reference from http://www.thaiall.com/class/TestBubbleSort.java
import java.lang.*;
class aaa {
int elems=0;
Comparable ar[] = new Comparable[2];
public void insert(Comparable value) {
ar[elems] = value;
elems++;
}
public void printresult() {
System.out.println(ar[0]+"|"+ar[1]); // 1|2
}
}
class Testa {
public static void main(String[] args) {
aaa arr = new aaa();
arr.insert(new Integer(1));
arr.insert(new Integer(2));
arr.printresult();
}
}
// Testb.java
// Reference from http://www.thaiall.com/class/TestBubbleSort.java
import java.lang.*;
class Testb {
static int elems=0;
static Comparable ar[] = new Comparable[2];
public static void main(String[] args) {
insert(new Integer(1));
insert(new Integer(2));
printresult();
}
static void insert(Comparable value) {
ar[elems] = value;
elems++;
}
static void printresult() {
System.out.println(ar[0]+"|"+ar[1]); // 1|2
}
}
class xxx {
void prtxx() {
System.out.println("xx");
}
}
class x {
public static void main(String args[]) {
System.out.println("x");
new xxx().prtxx();
xxx a = new xxx(); // ไม่มี constructor ใน xxx
a.prtxx();
}
}
// Result of this program
// x
// xx
// xx
class xxx {
void prtxx() {
System.out.println("xx");
}
xxx() {
System.out.println("ddd");
}
}
class x {
public static void main(String args[]) {
System.out.println("x");
new xxx().prtxx(); // ทำงานทั้ง object และ constructor
xxx a = new xxx(); // ทำงานเฉพาะ constructor
a.prtxx(); // ทำงานเฉพาะ object
new xxx(); // ทำงานเฉพาะ constructor
}
}
// Result of this program
// x
// ddd
// xx
// ddd
// xx
// ddd
// x.java
class x {
public static void main(String args[]) {
subx();
}
static void subx() {
System.out.println("hen");
}
}
// y.java
class y {
public static void main(String args[]) {
System.out.println("rat");
x a = new x();
a.subx();
}
}
// Result of this program
// rat
// hen
// x.java
import java.util.*;
import java.lang.*;
class a {
int b;
a() {
p("Call object a");
}
a(int i) {
p("Number is " + i);
}
void c() {
p("Receive nothing");
}
void c(int i) {
p("Number = " + i);
}
void c(String d) {
p("Word = " + d);
}
static void p(String s) {
System.out.println(s);
}
}
class x {
public static void main(String args[]) {
a n = new a(2);
n.c();
n.c(3);
n.c("This is test");
new a();
}
}
// Result of this program
// Number is 2
// Receive nothing
// Number = 3
// Word = This is test
// Call object a
// a1.java : รุ่นปู่ กำหนดค่า แล้วพิมพ์ 2 บรรทัด
class a1 {
static int i;
public static void main(String args[]) {
seti();
printnumber(i);
printline();
}
static void seti() {
i = 1;
}
static void printnumber(int j) {
System.out.println("Number = " + j);
}
static void printline() {
System.out.println("==========");
}
}
// a2.java : รุ่นพ่อ กำหนดค่า i เอง ไม่ใช้ seti() และไม่พิมพ์เส้น
public class a2 extends a1 {
static int i = 2;
public static void main (String[] args) {
printnumber(i);
}
}
// a3.java : รุ่นหลาน เปลี่ยนรูปแบบเส้น และไม่ใช้ i จากรุ่นก่อน
public class a3 extends a2 {
public static void main(String args[]) {
printnumber(3);
printline();
}
static void printline() {
System.out.println("----------");
}
}
// a4.java : รุ่นเหลน แก้ seti ให้ return type ไม่ได้
public class a4 extends a3 {
public static void main(String args[]) {
seti();
printnumber(i);
printline();
}
static void seti() {
// override หรือสืบทอดเปลี่ยนได้เฉพาะ code
i = setinew();
}
static int setinew() {
i = 4;
return i;
}
}
// a.java
public class a {
a() {
System.out.println("a constructor");
}
}
// b.java
public class b extends a {
b(int i) {
System.out.println("b constructor " + i);
}
public static void main (String[] args) {
b xxx = new b(100);
}
}
// Result of this program
// a constructor
// b constructor 100
// a.java is superclass
public class a {
a() {
System.out.println("a constructor");
}
void aa() {
System.out.println("aa in class a");
}
}
// b.java is superclass of c.java and subclass of a.java
public class b extends a {
b() {
System.out.println("b constructor");
}
void aa() {
System.out.println("aa in class b");
}
}
// c.java is subclass of b.java
public class c extends b {
c() {
System.out.println("c constructor ");
}
void aa() {
System.out.println("aa in class c");
super.aa(); // call aa in superclass
new a().aa();
}
public static void main(String args[]) {
c d = new c();
d.aa();
// aa(); can not run because aa should be static
new a().aa();
}
}
// Result of this program
// a constructor
// b constructor
// c constructor
// aa in class c
// aa in class b
// a constructor
// aa in class a
// a constructor
// aa in class a
// a.java
class a {
a() {
System.out.println("a constructor");
}
}
// b.java
public class b {
public a aa = new a();
public static void main (String args[]) {
b x = new b();
}
b() {
System.out.println("b constructor");
}
}
// Result of this program
// a constructor
// b constructor
// a.java
class a {
a() {
System.out.println("a constructor");
}
}
// b.java
public class b {
public a aa;
public static void main (String args[]) {
b x = new b();
}
b() {
System.out.println("b constructor");
new a();
}
}
// Result of this program
// b constructor
// a constructor
// ========================
// Technique of composition
// ========================
// wheel.java
class wheel {
private int a;
private int b;
public car thecar;
wheel() {
System.out.println("constructor wheel() from class wheel");
}
public void putwheel() {
System.out.println("putwheel() from class wheel");
}
}
// เมื่อแปล door จะแปล car ด้วย
// door.java
class door {
private int x;
private int y;
public car thecar;
door() {
System.out.println("constructor door() from class door");
}
public void putdoor() {
System.out.println("cleandoor() from class door");
}
}
// แปล car จะแปล และได้ wheel.class และ door.class
// car.java
public class car {
public wheel thewheel = new wheel();
public door thedoor = new door();
public static void main (String args[]) {
car x = new car();
x.thewheel.putwheel();
x.thedoor.putdoor();
}
car() {
System.out.println("Constructor car()");
}
public void drive() {
System.out.println("drive()");
}
}
// Result of this program
// constructor wheel() from class wheel
// constructor door() from class door
// Constructor car()
// putwheel() from class wheel
// cleandoor() from class door
// x.java : เรียก a1 ซึ่งเป็นลูกของ a มาทำงาน
class a {
a() {
System.out.println("a");
}
static void a22() {
System.out.println("oh no");
}
}
class a1 extends a {
a1() {
System.out.println("a1");
}
}
class a2 extends a {
a2() {
System.out.println("a2");
}
static void a22() {
System.out.println("a22");
}
}
public class x {
public static void main (String args[]) {
a xxx1 = new a1();
a xxx2;
xxx2 = new a2();
a2 xxx3 = new a2();
xxx3.a22();
}
}
// Result of this program
// a
// a1
// a
// a2
// a
// a2
// a22
import java.awt.*;
import java.awt.event.*;
public class x implements ActionListener {
Frame s = new Frame("Screen");
Button bclose = new Button("Exit");
public static void main(String[] args) {
new x().init();
}
public void init( ) {
s.setSize(150,150);
s.setLayout(null); // ถ้าไม่มี setLayout หรือ setVisible
s.setVisible(true); // จะกลายเป็นปุ่มเต็มจอ
s.setBackground(Color.yellow);
bclose.setBounds(10,40,70,20);
s.add(bclose);
bclose.addActionListener(this);
s.show();
}
public void actionPerformed(ActionEvent a) {
if(a.getSource()==bclose) { System.exit(0); }
}
}
// การ compile และ run ก็ใช้ javac และ java ตามปกติ แต่โปรแกรมจะเรียก appletviewer มาทำงานเอง
import java.awt.*;
import java.awt.event.*;
public class x implements ActionListener {
Frame s = new Frame("Screen");
Button bclose = new Button("Exit");
Button badd = new Button("Add");
TextField txt = new TextField(10);
public static void main(String[] args) {
new x().init();
}
public void init( ) {
s.setSize(150,150);
s.setLayout(null);
s.setVisible(true);
s.setBackground(Color.yellow);
txt.setBounds(10,40,70,20);
badd.setBounds(10,70,70,20);
bclose.setBounds(10,100,70,20);
s.add(txt);
s.add(badd);
s.add(bclose);
badd.addActionListener(this);
bclose.addActionListener(this);
txt.setText("0");
s.show();
}
public void actionPerformed(ActionEvent a) {
String aa = Double.toString(Double.parseDouble(txt.getText()) + 5);
// String aa = Integer.toString(Integer.parseInt(txt.getText()) + 5);
if(a.getSource()==badd) { txt.setText(aa); }
if(a.getSource()==bclose) { System.exit(0); }
}
}
import java.awt.*;
import java.awt.event.*;
public class x implements ActionListener {
Frame s1 = new Frame("Screen1");
Frame s2 = new Frame("Screen2");
Button bclose = new Button("Exit");
Button bshow2 = new Button("show2");
public static void main(String[] args) {
new x().init();
}
public void init( ) {
s1.setSize(150,150);
s1.setLayout(null);
s1.setBackground(Color.yellow);
s2.setSize(150,150);
s2.setLayout(null);
s2.setBackground(Color.green);
bshow2.setBounds(10,70,70,20);
bclose.setBounds(10,100,70,20);
s1.add(bshow2);
s1.add(bclose);
bshow2.addActionListener(this);
bclose.addActionListener(this);
s1.setVisible(true);
// s1.show();
}
public void actionPerformed(ActionEvent a) {
if(a.getSource()==bshow2) {
s1.setVisible(false);
s2.add(bclose);
bclose.addActionListener(this);
s2.show();
}
if(a.getSource()==bclose) System.exit(0);
}
}
// yonok1.java
package burin;
public class yonok1 {
public yonok1() {
System.out.println("YONOK");
}
}
// yonok2.java
package burin;
public class yonok2 {
public yonok2() {
System.out.println("Burin Rujjanapan");
}
public void prt(String s){
System.out.println(s);
}
}
// import testpackage.java
import burin.*;
class testpackage {
public static void main (String[] args) {
yonok1 xxx = new yonok1();
yonok2 yyy = new yonok2();
yyy.prt("test of package");
}
}
// Result of this program
// YONOK
// Burin Rujjanapan
// test of package
// x.java
abstract class workinfile {
public abstract void edit();
public abstract void show();
public void display(){
System.out.println("display");
}
}
class w extends workinfile {
public void edit(){
System.out.println("edit");
}
public void show(){
System.out.println("show");
}
}
public class x {
public static void main (String args[]) {
w aaa = new w();
aaa.edit();
aaa.show();
aaa.display();
}
}
// Result of this program
// edit
// show
// display
interface a {
int bb = 10;
public void show();
public int i();
}
class c implements a {
public void show() {
System.out.println("show");
}
public int i() { return (bb + 5); }
}
public class x {
public static void main (String args[]) {
c aaa = new c();
aaa.show();
System.out.println(aaa.i());
}
}
// Result of this program
// show
// 15
package a;
public class b{
class x {
public static void main(String args[] {
System.out.println("aa");
}
}
or
class x {
public static void main(String args[]) {
for(int j=5;j>-5; j--) {
System.out.println(5/j); เกิด arithmetic error ขณะ run-time
}
}
}
class x {
public static void main(String args[]) {
System.out.println("===== start =====");
try {
for(int j=5;j>-5; j--) {
System.out.println(7/j);
}
} catch (ArithmeticException e) {
System.out.println("You divide by zero");
}
System.out.println("===== finish =====");
}
}
// Result of this program
// ===== start =====
// 1
// 1
// 2
// 3
// 7
// You divide by zero
// ===== finish =====
class x {
public static void main(String args[]) {
System.out.println("===== start =====");
for(int j=2;j>-2; j--) {
try {
System.out.println(7/j);
} catch (ArithmeticException e) {
System.out.println("You divide by zero");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error on array");
} finally {
System.out.println("Each case");
}
}
System.out.println("===== finish =====");
}
}
// Result of this program
// ===== start =====
// 3
Each case
// 7
Each case
// You divide by zero
Each case
// -7
Each case
// ===== finish =====
class y {
int z = 5;
}
class x {
public static void main(String args[]) {
// y a = new y();
y a,b;
a.z = 1;
System.out.println(a.z + b.z); // 6
}
}
class y {
int z1 = 5;
static int z2 = 4;
}
class x extends y{
public static void main(String args[]) {
y a1 = new y();
y a2 = new y();
a1.z1 = 3;
System.out.println(a1.z1 + a2.z1); // 8
z2 = 1;
System.out.println(a1.z2 + a2.z2 + z2); // 3
}
}
<applet code="j11.class" width=100 height=100> </applet>
import java.io.*;
class ar5max {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int buf[] = new int[5];
int i,max=0;
for (i=0;i<5;i++) {
buf[i] = Integer.parseInt(stdin.readLine());
}
for (i=0;i<5;i++) {
if (buf[i] > max) { max = buf[i]; }
}
System.out.println("Max = " + max);
}
}
import java.lang.*;
class x{
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
System.out.println("Total memory is: " + r.totalMemory());
System.out.println("1 Free memory: " + r.freeMemory());
r.gc();
System.out.println("2 Free memory: " + r.freeMemory());
Integer someints[] = new Integer[10];
System.out.println("3 Free memory: " + r.freeMemory());
for(int i=0; i<10; i++) {
someints[i] = new Integer(i); // allocate integers
}
System.out.println("4 Free memory: " + r.freeMemory());
for(int i=0; i<10; i++) someints[i] = null;
System.out.println("5 Free memory: " + r.freeMemory());
r.gc();
System.out.println("6 Free memory: " + r.freeMemory());
}
}
// Total memory is: 2031616
// 1 Free memory: 1916848 (- 114768)
// 2 Free memory: 1946920 (+ 30072)
// 3 Free memory: 1946304 (- 616)
// 4 Free memory: 1945872 (- 432)
// 5 Free memory: 1945600 (- 272)
// 6 Free memory: 1946864 (+ 1264)
import java.lang.*;
class x{
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
long mem1, mem2;
Integer someints[] = new Integer[1000];
System.out.println("Total memory is: " + r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free memory: " + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println("Free memory after garbage collection: " + mem1);
for(int i=0; i<1000; i++) {
someints[i] = new Integer(i); // allocate integers
}
mem2 = r.freeMemory();
System.out.println("Free memory after allocation: " + mem2);
System.out.println("Memory used by allocation: " + (mem1-mem2));
// discard Integers
for(int i=0; i<1000; i++) someints[i] = null;
r.gc(); // request garbage collection
mem2 = r.freeMemory();
System.out.println("Free memory after collecting & discarded Integers: " + mem2);
}
}
// http://www.java-samples.com/showtutorial.php?tutorialid=231