public class Test {
public static void main(String[] args) {
final String str1 = "HelloWorld";
final String str2 = "Hello" + "World";
System.out.println(str1 == str2);//true
final String str3 = "Hello" + String.valueOf("World");
System.out.println(str1 == str3);//false
}
}
public class Test {
final String str1 = "HelloWorld";
final String str2 = "Hello" + "World";
final String str3;
final String str4;
{
str3 = "HelloWorld";
}
{
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//true
// System.out.println(str1 == str4);//compile error
}
public Test() {
str4 = "HelloWorld";
System.out.println(str1 == str4);//true
}
public static void main(String[] args) {
new Test();
}
}
public class Test {
final String str1;//compile error---没有显示的使用①②③中的方式进行初始化
String str2;
}
public class Test {
final String str1 = "Hello";//定义实例变量时指定初始值
final String str2;//非静态初始化块中对实例变量进行初始化
final String str3;//构造器中对实例变量进行初始化
{
str2 = "Hello";
}
public Test() {
str3 = "Hello";
}
public void show(){
System.out.println(str1 + str1 == "HelloHello");//true
System.out.println(str2 + str2 == "HelloHello");//false
System.out.println(str3 + str3 == "HelloHello");//false
}
public static void main(String[] args) {
new Test().show();
}
}
class Fruit{
String color = "unknow";
public Fruit getThis(){
return this;
}
public void info(){
System.out.println("fruit's method");
}
}
public class Apple extends Fruit{
String color = "red";//与父类同名的实例变量
@Override
public void info() {
System.out.println("apple's method");
}
public void accessFruitInfo(){
super.info();
}
public Fruit getSuper(){
return super.getThis();
}
//for test purpose
public static void main(String[] args) {
Apple a = new Apple();
Fruit f = a.getSuper();
//Fruit f2 = a.getThis();
//System.out.println(f == f2);//true
System.out.println(a == f);//true
System.out.println(a.color);//red
System.out.println(f.color);//unknow
a.info();//"apple's method"
f.info();//"apple's method"
a.accessFruitInfo();//"fruit's method"
}
}
class Fruit{
String color;
public Fruit() {
color = this.getColor();//父类color属性初始化依赖于重载的方法getColor
// color = getColor();
}
public String getColor(){
return "unkonw";
}
@Override
public String toString() {
return color;
}
}
public class Apple extends Fruit{
@Override
public String getColor() {
return "color: " + color;
}
// public Apple() {
// color = "red";
// }
public static void main(String[] args) {
System.out.println(new Apple());//color: null
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有