5.5.3this表示当前对象
一个类可以实例化出若干个对象,这些对象都可以调用类中提供的方法,n那么对于当前正在访问类中方法的对象就可以称为当前对象,而this就可以描述出这种当前对象的概念。
范例:观察当前对象
public class EightEight_a {
public void printThis() {
System.out.println("[EightEight_a]="+this);
}
}
public class EightEight_b {
public static void main(String[] args) {
EightEight_a msgA=new EightEight_a();
System.out.println("[主类]="+msgA);
msgA.printThis();
System.out.println("-------------------");
EightEight_a msgB=new EightEight_a();
System.out.println("[主类]="+msgB);
msgB.printThis();
}
}
[主类]=com.lxh.fivachapter.EightEight_a@15db9742
[EightEight_a]=com.lxh.fivachapter.EightEight_a@15db9742
-------------------
[主类]=com.lxh.fivachapter.EightEight_a@6d06d69c
[EightEight_a]=com.lxh.fivachapter.EightEight_a@6d06d69c
范例:实现消息发送逻辑
public class EightyNine_a { //消息类
private EightyNine_b pass;
private String title;
private String content;
public void setPass(EightyNine_b pass) {
this.pass =pass;
}
public EightyNine_b getPass() {
return pass;
}
public void setTitle(String title) {
this.title =title;
}
public String getTitle() {
return title;
}
public void setContent(String content) {
this.content =content;
}
public String getContent() {
return content;
}
public EightyNine_a() {
}
public EightyNine_a(EightyNine_b pass,String title,String content) {
this.pass =pass;
this.title =title;
this.content =content;
}
public void send() {
if(this.pass .isContent()) {
System.out.println("发送消息为"+title+"/n"+content);
}else {
System.out.println("无消息发送");
}
}
}
class EightyNine_b { //通道类
private EightyNine_a massage;
public EightyNine_a getMassage() {
return massage;
}
public void setMassage(EightyNine_a massage) {
this.massage = massage;
}
public EightyNine_b(String title,String content) {
this.massage=new EightyNine_a(this,title,content);//this=ch
this.massage.send();
}
public boolean isContent() {
return true;
}
}
class EightyNine_c {
public static void main(String[] args) {
//传入要发送的内容
EightyNine_b ch=new EightyNine_b("hahah","uwibcwb");
}
}
发送消息为hahah/nuwibcwb
消息发送需要通道实现,所以需实例化通道,将当前对象this传递到消息类,用消息类.send()方法实现消息发送处理。
5.5.4综合案例 简单java类
一种信息保存的媒介存在,核心开发结构如下:
- 类名称一定要有意义,可以明确的描述某一类事物。
- 类中的所有属性都必须使用private进行封装,封装后的属性必须提供setter(),getter()方法。
- 类中可以提供有无数多个构造方法,但是必须保留无参构造方法。
- 类中不允许出现任何输出语句,所有内容获取必须返回。
- 【可选】可以提供一个获取对象详细信息的方法,暂时将此方法名称定义为getInfo()。
范例:定义一个描述部门的简单类。
public class Dept {
private int number;
private String name;
private String loc;
public void setNumber(int number) {
this.number=number;
}
public int getNumber() {
return number;
}
public void setNume(String name) {
this.name=name;
}
public String getNume() {
return name;
}
public void setLoc(String loc) {
this.loc =loc;
}
public String getLoc() {
return loc;
}
public Dept() {
}
public Dept(int number,String name,String loc) {
this.number =number;
this.name=name;
this.loc =loc;
}
public String getInfo() {
return "部门编号"+this.number +"部门名称"+this.name+"部门归属"+this.loc;
}
}
class Dept_a {
public static void main(String[] args) {
Dept de=new Dept(1,"社团活动部","社团联合会");
System.out.println(de.getInfo());
}
}
部门编号1部门名称社团活动部部门归属社团联合会
本文探讨了Java中this关键字的使用,展示了如何通过this引用当前对象,以及在消息发送逻辑和类设计中的应用。通过具体示例,阐述了类的属性封装、构造方法和信息获取方法的重要性。
1197

被折叠的 条评论
为什么被折叠?



