• 学习笔记 Java类的封装、继承和多态 2014.7.10


    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


     

  • 相关阅读:
    005.hive分组求中位数--sort_array(collect_list(),insert overwrite table --- 实现字段update操作
    004.hive 里的向上取整、向下取整、四舍五入取整
    003.hive选择tableName表中除了name、id、pwd之外的所有字段
    003.flink-flink集群安装HA
    002.flink打jar包时只打包代码,依赖jar包批量指定
    001.如何通过进程号查看进程所依赖的jar包,cd /proc/92232/fd
    002.hive-UDF自定义函数
    02.spark-spark分布式安装
    052.PGSQL-search_path参数 修改了postgresql.conf没有生效 ,应该修改这个配置postgresql.auto.conf文件,postgresql.auto.conf修改系统文件。配置是否被成功应用 pg_file_settings
    devexpress gridview样式设置
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6816399.html
Copyright © 2020-2023  润新知