เขียนแทบจะเหมือนกัน และด้วย concept oop ที่แน่นมากทำให้ลักษณะ และคุณสมบัติ ของทั้ง สองภาษานี้ เหมือนกันเลย
แล้วที่ต่างกันอะ น่านสิ ลองดูเล่น ๆ กันดีกว่า
อย่างแรกเลยคงเป็น package หรือลำดับการเข้าถึง
java : เนื่องด้วยการเก็บ file ไว้ใน folder จริงทำให้มีการอ้างถึงตาม folder ที่อยู่
เช่น th >> co >> google >> code
package th.co.google.code // how to use import th.co.google.code.Clazz
vc# : ในส่วนของ vc# นั้นจะต่างกันเนื่องจากว่า เป็นแค่การอ้างถึงการเข้าถึงจริง ๆ
ไม่ได้มีการแยกที่อยู่ไว้อย่างไร และจะเป็นการห่อหุ้ม Code ที่ต้องการประกาศการเข้าถึง
namespace th.co.google.code{
// insert class hear
}
// or
namespace th{
namespace co{
namespace google{
namespace code{
// insert class hear
}
}
}
}
// how to use
using th.co.google.code.Clazz
concept get-set ในกรณี
java :
public class Clock{
private int hour;
public int getHour(){
return hour;
}
public void setHour(int value){
hour = value;
}
}
// how to use
Clock c = new Clock();
c.setHour(8);
System.out.println(c.getHour());
vc#:
public class Clock{
public int Hour {
get {
return hour;
}
set {
hour = value;
}
}
}
// how to use
Clock c = new Clock();
c.Hour = 8;
Console.WriteLine(c.Hour);
จะเห็นได้ว่า ภาษา java ค่อนข้างจะเก็บ concept เดิมไว้ได้ดี
แต่ภาษา c# นี่ยัดกันเป็นรูปแบบแปลก ๆ ขึ้นมาเชียว -_-" เหมือนทำ Inner class เลย...
การ สืบทอดคุณสมบัติจาก class แม่ อันนี้ต่างกันนิดหน่อย
java :
public class Parent{
public void print(){
System.out.println("== Parent ==");
}
}
public class Child extends Parent{
public void print(){
System.out.println("== Child ==");
}
public void printAll(){
this.print();
super.print();
}
}
// how to use
Child c = new Child();
c.printAll();
vc# :
public class Parent{
public void print(){
System.out.println("== Parent ==");
}
}
public class Child : Parent{
public void print(){
System.out.println("== Child ==");
}
public void printAll(){
this.print();
base.print();
}
}
// how to use
Child c = new Child();
c.printAll();
และ การรับ interface ที่แยกไม่ออกจริง ๆ พี่น้อง ^-^"
java :
public interface Interfazz{
public void printTest();
}
public class Clazz implements Interfazz {
public void printTest(){
System.out.println("Clazz from Interfazz");
}
}
vc# :
public interface Interfazz{
public void printTest();
}
public class Clazz : Interfazz {
public void printTest(){
Console.WriteLine("Clazz from Interfazz");
}
}
...
ไม่มีความคิดเห็น:
แสดงความคิดเห็น