1、
问题:toString()没搞懂?
int a = 1;
Integer aa = new Integer(a); //这是实现的过程
System.out.println("Hello," + aa.toString()); //得Hello,1
int b = 1;
System.out.println("Hello," + b); //得Hello,1
class MyDate
{
int year,month,day;
public int get()
{
return year;
}
public static void main(String[] args)
{
MyDate d1 = new MyDate();
System.out.println(d1); //打印类名的时候,需要复写父类的toString()方法
System.out.println(d1.get()); //打印类中的方法时,不用复写,因为方法里面有自己写的返回。
}
}
2、代码:MyDate.java 通过声明MaDate类,演示类的声明格式、创建对象、引用对象成员变量、调用对象成员方法等语法。
public class MyDate { //类声明
int year, month, day; //成员变量,表示年、月、日
void set(int y, int m, int d){ //成员方法,设置日期值
this.year = y;
this.month = m;
this.day = d;
}
void set(MyDate d){ //将当前对象值设为参数值,重载
set(d.year, d.month, d.day);
}
public String toString(){ //返回中文日期字符串
return year + "年" + month + "月" + day + "日";
}
public boolean equals(MyDate d){
return this == d || d != null && year == d.year && month == d.month && day == d.day;
//&&的优先级好像比||高。this == d ||表示如果d1和d2引用同一个实例,那么d1和d2的值就相等,就直接返回true(this:指代调用成员方法的当前对象本身;this.year:访问本类的成员变量和成员方法)
}
public static void main(String[] args) {
MyDate d1 = new MyDate(); //声明对象、创建实例、引用赋值
d1.set(1992, 10, 13); //调用类的成员方法
MyDate d2 = d1; //对象引用赋值
System.out.println("d1:" + d1.toString() + ", d2:" + d2 + ", d1 == d2?" + (d1 == d2) + " d1.equals(d2)?" + d1.equals(d2));
//==、!=判断d1和d2是否引用同一个实例。equals()比较d1和d2的值是否相等
//为什么如果不加public String toString(),这里就会出现哈希那一坨(它怎么会去调用那个函数呢?) .toString()可以省略
//得d1:1992年10月13日, d2:1992年10月13日, d1 == d2?true d1.equals(d2)?true
d2.year = 2014; //修改实例成员变量值
System.out.println("d1:" + d1 + ", d2:" + d2 + ", d1 == d2?" + (d1 == d2) + " d1.equals(d2)?" + d1.equals(d2));
//得d1:2014年10月13日, d2:2014年10月13日, d1 == d2?true d1.equals(d2)?true
d2 = new MyDate(); //创建另一个实例
d2.set(d1);
System.out.println("d1:" + d1 + ", d2:" + d2 + ", d1 == d2?" + (d1 == d2) + " d1.equals(d2)?" + d1.equals(d2));
//得d1:2014年10月13日, d2:2014年10月13日, d1 == d2?false d1.equals(d2)?true
}
}
3、代码:MyDate_Constructor.java 类的构造方法
public class MyDate_Constructor {
int year; //成员变量
public MyDate_Constructor() { //默认构造方法
// year = 1992;
this(1992); //this:调用本类已定义的构造方法
}
public MyDate_Constructor(int y) { //声明构造方法,初始化成员变量
year = y;
}
public MyDate_Constructor(MyDate_Constructor t) { //拷贝构造方法,创建新实例,值同参数实例
// year = t.year;
this(t.year);
}
public String toString() {
return year + " ";
}
public static void main(String[] args) {
MyDate_Constructor t1 = new MyDate_Constructor();
System.out.println(t1); //得1992
MyDate_Constructor t2 = new MyDate_Constructor(2014); //得2014
System.out.println(t2);
MyDate_Constructor t3 = new MyDate_Constructor(t1); //调用拷贝方法复制实例。这句话功能相当于test1 t3 = new test1(); t3.set(t1);
System.out.println(t3); //得1992
}
}
4、问题:instanceof运算符怎么用?
MyDate d = new MyDate();
d instanceof MyDate //结果是true,d是MyDate类的实例。怎么运行出来?
class test
{
}
class test_1
{
}
class test_ex
{
public static void main(String[] args)
{
test t = new test();
boolean b1 = t instanceof test;
System.out.println(b1); //得true
/*
test_1 t1 = new test_1();
boolean b2 = t1 instanceof test; //错误:t1不可转换的类型
System.out.println(b2);
*/
}
}
5、问题:对4个访问权限的作用范围不理解?
6、Java类中的成员分为两种:实例成员(属于对象,要创建实例,才能访问)和静态成员(属于类,没创建实例,也可以访问)。
问题:静态成员怎么用?
7、问题:一个源程序文件里面包含两个类为什么有错啊(Myeclipse 10)? 在控制台上都可以运行,编译生成两个字节码文件test.class和test_1.class