zoukankan      html  css  js  c++  java



  • Object

    所有类的超类,没有extends的类默认继承Object,其中方法适合所有类。


    toString()
    public String toString()
    {
    <span style="white-space:pre">	</span>getClass().getName() + '@' + Integer.toHexString(hashCode())
     
    }
    

    class Obj                //默认继承Object
    {
    }
    class Demo
    {
    <span style="white-space:pre">	</span>public static void main(String[] args)
    {
    <span style="white-space:pre">	</span>Obj o=new Obj();
    <span style="white-space:pre">	</span>System.out.println(o);   //相当于System.out.println(o.toString());
    }
    }


    内部类


    类中可以定义类,内部类可以被public和static修饰。

    内部类可以访问外部类的成员,外部类不能访问内部类成员。

    static修饰后的内部则变成外部类,不能访问外部内非静态成员。


    内部类创建对象从外部类之外调用


    Outer o=new Outer();         //Outer为外部类,Inner为内部类。
    Outer.Inner i=o.new Inner();
    i.show();                    //show为内部类方法。


    方法内部类访问方法中的变量,其变量必被final修饰。



    内部类是可以继承接口和父类的。




    匿名内部类

    interface Student
    {
    	void say();
    }
    class Person
    {
    	void get(Student s)
    	{
    		s.say();              //其实就是多态。
    	}
    	
    	
    	public void test()
    	{
    	
    		get(new Student()         //创建一个继承Student的匿名对象,其方法一起传到get方法中。
    			{
    				public void say()
    				{
    					System.out.println("I'm a student.");
    				}
    			}
    		   );
    	}	
    
    }
    class PersonDemo
    {
    	public static void main(String[] args)
    	{
    		Person p =new Person();
    		p.test();
    		
    	}
    }




  • 相关阅读:
    yum下载安装mysql服务
    windows编写sh脚本在linux上不能执行
    ectouch第二讲之 文件结构
    ecshop第一讲之安装
    class id 区别
    thinkphp模板中截取中文字符串的方法分享
    CI 配置验证规则
    CodeIgniter配置之config
    codeigniter中base_url和site_url
    使用phpmyadmin导入SQL数据报错:#1062
  • 原文地址:https://www.cnblogs.com/lisisong/p/5122625.html
Copyright © 2011-2022 走看看