黑马程序员---面向对象关键字 this super static final

基类: BaseTest
public class BaseTest {
protected String pwd; // 定义protected属性pwd
public BaseTest(){
// 无参数构造方法
}
public String sayHelloWorld(){
return "hello world";
}
}

/**
* @author Administrator
*
* @description this,super关键字测试类
* @history

*/
public class MyTestDemo extends BaseTest{
// notes:测试使用而已,代码功能模拟而已
private String username; // 定义用户昵称私有属性private修饰
private String password;
public MyTestDemo(String username){ // 构造方法
this();// this()调用无参数构造方法
this.username = username; // this.属性访问当前username属性
// super.属性调用父类属性
this.password = super.pwd;
}
public MyTestDemo(){
// 无参数构造方法
super(); // 调用父类无参数构造方法
}
public void sayHello(){
this.sayWord(); // this.方法调用当前对象的sayWord方法
super.sayHelloWorld(); // super.方法调用父类sayHelloWorld方法
}
public String sayWord(){
return "hello this";
}
// this表示当前对象
public boolean equals(Object obj){
MyTestDemo other = (MyTestDemo)obj; // 传入的比较对象,强制转换操作
// 简单模拟,如果对象的username属性值相同那么就认为相同
if(this == other){
return true;
}
if(other instanceof MyTestDemo){
if(this.username.equals(other.username)){
return true;
}
}
return false;
}
/**
*@description
*@param args
*/
public static void main(String[] args) {
// this关键字绑定了当前该对象,通过this.属性、 this.方法
// super关键字用在子类要调用父类的属性或者方法中,通过super.属性、 super.方法
MyTestDemo obj1 = new MyTestDemo("hello-java"); // 定义并实例化对象

obj1.sayHello(); // 调用对象的sayHello方法
MyTestDemo obj2 = new MyTestDemo("hello-java");
// equals方法传入的参数类型为object,传入的obj2会自动向上转型处理
boolean result = obj1.equals(obj2);
System.out.println(result); // true
}
}

_____________________________________________________________________

/**
* @author Administrator
*
* @description final和static关键字学习
* @history
*/
public class FinalAndStaticTestDemo {
/**
*@description
*@param args
*/
public static void main(String[] args) {
// final关键字主要有三种场景
// 1、 final定义的变量为常量,比如基本数据类型的值一旦初始化了不能改变
// 2、 final定义的类为终结类,不能再被继承,比如string类型
// 3、 final定义的方法为终结方法,不能被覆写,否则编译器会报错
final String str = "helloworld"; // final修饰变为常量不能改变
//str = "hellojava"; // eclipse提示:The final local variable str cannot be
assigned.
// 进一步思考下面两个问题
String s1 = "hello";
String s2 = "hello1";
final String s = s1;
s1 = s2;
System.out.println(s); // hello还是hello1呢
// 引用不可变,内容可变是这个意思吗?
StringBuffer sb = new StringBuffer();
sb.append("hello");
final String fs = sb.toString();
final StringBuffer fsb = sb;
sb.append("world");
System.out.println(fs);// hello还是helloworld呢

System.out.println(fsb.toString()); // hello还是helloworld呢
// static关键字比较类似也有下面三种场景
// static修饰的变量为静态变量,它不同于对象/实例变量,是关联在某个类上的
// static修饰的类为静态类
// static修饰的方法为静态方法,它也不同于对象/实例方法,是关联在某个类上的
// static int i = 100;
// static class HelloWorld{ //... }
// public static void sayHeloWorld(){ //... }
}
// 静态变量和对象变量
static int i = 100; // 定义静态变量i
int j = 100; // 定义成员变量/实例变量
public static void sayHelloWorld(){ // 定义静态方法sayhelloworld
System.out.println("hello world");
}
static class HelloWorld{ // 定义内部类
// ...
}
}

 

转载于:https://www.cnblogs.com/castworld/p/4868777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值