Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 9 của 9
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Từ khóa Super trong Java dùng để làm gì?

    Mọi người cho mình hỏi từ khóa Super trong java dùng để làm gì với[IMG]images/smilies/17.gif[/IMG][IMG]images/smilies/17.gif[/IMG][IMG]images/smilies/17.gif[/IMG]

    vd trong trường hợp sau nó để làm gì

    Mã:
    public class UserImpl extends BasicImpl implements User {    public UserImpl(ConnectionPool cp) {        super(cp, "User");    }
    Thank!!!!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    super nghĩa là Super class (lớp được kế thừa hoặc lớp cha) tương đương với base trong C#, được sử dụng trong kế thừa.
    super(cp, "User"); nghĩa là gọi constructor của lớp cha

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi shintani
    Mọi người cho mình hỏi từ khóa Super trong java dùng để làm gì với[IMG]images/smilies/17.gif[/IMG][IMG]images/smilies/17.gif[/IMG][IMG]images/smilies/17.gif[/IMG]

    vd trong trường hợp sau nó để làm gì

    Mã:
    public class UserImpl extends BasicImpl implements User {    public UserImpl(ConnectionPool cp) {        super(cp, "User");    }
    Thank!!!!
    Như trong code trên thì

    Mã:
    super(cp, "User");
    sẽ gọi hàm tương ứng của lớp cha BasicImpl, tức là hàm đó sẽ tương đương với
    Mã:
    BasicImpl(cp, "User");

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    - Từ khóa this là ở class hiện tại còn super là lớp cha của lớp đó(tức lớp mà lớp hiện tại kế thừa). Dùng this khi gọi tới các method hoặc constructor của lớp hiện tại, super khi gọi tới method hoặc constructor của lớp cha, super bằng vs base trong C#. Bạn xem lại phần core cho rõ ràng.
    - Ví Dụ:

    Lớp BaseEntity làm nền cho tất cả các lớp thuộc package entity:


    Mã:
    package model.entity; import java.util.Date; /**** @author DucMjnh1992*/public abstract class BaseEntity {    private int id;    private Date createdDate;    private boolean deleted;     public int getId() {        return this.id;    }     public void setId(int id) {        this.id = id;    }     public Date getCreatedDate() {        return this.createdDate;    }     public void setCreatedDate(Date createdDate) {        this.createdDate = createdDate;    }     public boolean isDeleted() {        return this.deleted;    }     public void setDeleted(boolean deleted) {        this.deleted = deleted;    }     public BaseEntity(int id, Date createdDate, boolean deleted) {        this.id = id;        this.createdDate = createdDate;        this.deleted = deleted;    }     public BaseEntity() {        this(0, new Date(), false);    }}
    Lớp Category kế thừa thì BaseEntity:



    Mã:
    package model.entity; import java.io.Serializable;import java.util.Date; /**** @author DucMjnh1992*/public class Category extends BaseEntity  implements Serializable {    private String name;    private String description;     public String getDescription() {        return description;    }     public void setDescription(String description) {        this.description = description;    }     public String getName() {        return name;    }     public void setName(String name) {        this.name = name;    }     public Category(int id, String name, String description, Date createdDate, boolean deleted) {        super(id, createdDate, deleted);         this.name = name;        this.description = description;    }     public Category() {        this(0, "", "", new Date(), false);    }     @Override    public String toString() {        return this.name;    }}

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    mình chịu [IMG]images/smilies/17.gif[/IMG]
    Manchester United

    - - - Nội dung đã được cập nhật ngày 07-10-2014 lúc 04:22 PM - - -

    Cho mình hỏi nếu như ở class Category, ta không super id thì khi mình thực hiện this.id nó gọi id của lớp cha đúng ko ạ.

  6. #6
    Nó kế thừa rồi nên gọi this.<tên trường> thì nó cũng như gọi super.<tên trường> thôi.
    Nhưng trong một số trường hợp thì phải phân biệt rõ ràng, bên trong phương thức.
    - Khi gọi hàm dựng của lớp cha ở trong hàm dựng lớp con.
    - Khi gọi một phương thức của lớp cha đã bị lớp con ghi đè.

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    có 2 chỗ bạn thường dùng đến:
    1. Gọi lại contructor của lớp cha.

    Mã:
    class A{    A(int a){        System.out.println("Day la so " + a);    }}/ew A(1) in ra: Day la so 1;class B extend A{    B(int a, int b){        super(a);        System.out.print("va so " + b);    }}/ew B(1, 2) in ra: Day la so 1 va so 2;
    2. Khi override lại một hàm mà chỉ cần mở rộng từ hàm trước đó đã implement

    Mã:
    class A{    void Stupid(int a){        System.out.println("Day la so " + a);    }    //(new A()).Stupid(1); in ra: Day la so 1;}class B extend A{    @override    void Stupid(int a){        supper(a);        System.out.print("va binh phuong la " + (a * a));    }}//(new B()).Stupid(2) in ra: Day la so 2 va binh phuong la 4;

  8. #8
    Trích dẫn Gửi bởi DucMjnh1992
    - Từ khóa this là ở class hiện tại còn super là lớp cha của lớp đó(tức lớp mà lớp hiện tại kế thừa). Dùng this khi gọi tới các method hoặc constructor của lớp hiện tại, super khi gọi tới method hoặc constructor của lớp cha, super bằng vs base trong C#. Bạn xem lại phần core cho rõ ràng.
    - Ví Dụ:

    Lớp BaseEntity làm nền cho tất cả các lớp thuộc package entity:


    Mã:
    package model.entity; import java.util.Date; /**** @author DucMjnh1992*/public abstract class BaseEntity {    private int id;    private Date createdDate;    private boolean deleted;     public int getId() {        return this.id;    }     public void setId(int id) {        this.id = id;    }     public Date getCreatedDate() {        return this.createdDate;    }     public void setCreatedDate(Date createdDate) {        this.createdDate = createdDate;    }     public boolean isDeleted() {        return this.deleted;    }     public void setDeleted(boolean deleted) {        this.deleted = deleted;    }     public BaseEntity(int id, Date createdDate, boolean deleted) {        this.id = id;        this.createdDate = createdDate;        this.deleted = deleted;    }     public BaseEntity() {        this(0, new Date(), false);    }}
    Lớp Category kế thừa thì BaseEntity:



    Mã:
    package model.entity; import java.io.Serializable;import java.util.Date; /**** @author DucMjnh1992*/public class Category extends BaseEntity  implements Serializable {    private String name;    private String description;     public String getDescription() {        return description;    }     public void setDescription(String description) {        this.description = description;    }     public String getName() {        return name;    }     public void setName(String name) {        this.name = name;    }     public Category(int id, String name, String description, Date createdDate, boolean deleted) {        super(id, createdDate, deleted);         this.name = name;        this.description = description;    }     public Category() {        this(0, "", "", new Date(), false);    }     @Override    public String toString() {        return this.name;    }}
    Mình mới học java nên còn chưa rõ. Cho mình hỏi chỗ này mình sửa lại như thế này có đúng không nhỉ:
    Mã nguồn PHP:
    public Category(int id, String name, String description, Date createdDate, boolean deleted) { super(id, createdDate, deleted);  
    thành:
    Mã nguồn PHP:
    public Category(int id, String name, String description, Date createdDate, boolean deleted) { super.BaseEntity(id, createdDate, deleted);  
    Thank [IMG]images/smilies/smile.png[/IMG]

  9. #9
    Từ khóa của NNLT mà không biết dùng thì tốt hơn không dùng nó. Không quá 100 từ khóa mà không phân biệt được sao!

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •