`

instance、Class、Object

阅读更多
一、实例和类

所有实例都有所属的类;所有类都是Class类的实例

比如
public static void main(String[] args) {

		String s = "abc";
		System.out.println(s.getClass());
		System.out.println(s.getClass().getClass());

	}

输出是:
class java.lang.String
class java.lang.Class

在这个例子中,"abc"是String类的一个实例(s是这个实例的引用);而String类是Class类的一个实例

在任何实例上连续调用2次getClass()方法,得到的都是Class类。Class类比较特别,它是自身的一个实例

要获取一个实例所属的类,有2种办法,一种是在实例上调用getClass()方法,另一种是直接用.class属性,比如
System.out.println(String.class);


Class类定义了getClassLoader()和newInstance()方法,分别可以获取加载该类的ClassLoader,以及创建该类的一个实例

二、继承体系与Object

前面说的是“实例”和“类”的关系,不涉及到继承(extends关键字)

Object是所有类的基类,所有的类都隐性地extends Object,所以Object类上定义的所有方法,对于所有类都是可见的

Object类,也是Class类的一个实例
Object o = new Object();

在这里,new Object()在堆上创建了一个Object类的实例;而Object类本身,是Class类的一个实例

但是,Class也是extends Object的,所以也有在Object上定义的方法,如toString()、equals()等
分享到:
评论

相关推荐

    软件需求分析英文课件

    into an object object class object would sound a bit strange) Instance object an instance of a class object, instance, but not object instance Class object an instantiated class (everything...

    3D-RCNN: Instance-level 3D Object Reconstruction via Render-and-Compare

    the full 3D shape and pose of all object instances in the image. Our method produces a compact 3D representation of the scene, which can be readily used for applications like autonomous driving. Many ...

    又小又快又无依赖Json序列化与反序列化

    Object obj=JsonUtil.instance().json2Obj(json,Object.class) Object[]objs=JsonUtil.instance().json2Obj(json,Object[].class); //泛型 DatagridReturn<Object> dgr=JsonUtil.instance().json2Obj(json...

    Objective-C头文件导出工具class-dump

    -a show instance variable offsets -A show implementation addresses --arch <arch> choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64) -C <regex> only display classes ...

    Learning Rich Features from RGB-D Images for Object Detection and Segmentation

    existing superpixel classication framework for semantic scene segmenta- tion and achieve a 24% relative improvement over current state-of-the-art for the object categories that we study.We believe ...

    Oracle命令大全

    Object userDao = userDaoClass.newInstance(); Class userServiceClass = Class.forName(config .getInitParameter("userService")); Constructor cons1 = userServiceClass.getConstructor(UserDao...

    详解Ruby中的instance_eval方法及其与class_eval的对比

    这个BasicObject#instance_eval有点类似JS中的bind方法,不同的时,bind是将this传入到对象中,而instance_eval则是将代码块(上下文探针Context Probe)传入到指定的对象中,一个是传对象,一个是传执行体。...

    Mod5 – A Matlab Class Wrapper for MODTRA5 用于MODTRAN 5的Matlab类封装器

    These include methods to read the tape5 file format into a Mod5 class “instance” or “object”, alter the case instance described in the file, write the tape5 format back to a file, run MODTRAN on ...

    1程序设计模式.pdf

    程序设计模式 设计模式 设计模式 23个设计模式 - 宏观 单例模式(Singleton mode) # 两个实例 class A: pass a1 = A() a2 = A() print(a1) print(a2) # 结果 <__main__.A object at 0x01FE0890> <__main__.A object ...

    Python经典面试题.doc

    super(Singleton, cls).__call__(*args, **kw) return cls.instance class MyClass(object): __metaclass__ = Singleton print MyClass() print MyClass() 2. 使用decorator来实现单例模式 def singleton(cls): ...

    linux 网站搭建

    class Singleton(object): def __new__(cls, *args, **kwargs): if not hasattr(cls, 'instance'): cls.instance = super(Singleton, cls).__new__(cls) return cls.instance class MyClass(Singleton): ...

    Java 反射(Reflection) 经典实用例子

    public Object newInstance(String className, Object[] args) throws Exception { Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = ...

    设计模式C++代码示例-含VC工程

    A class of which only a single instance can exist Structural Patterns Adapter Match interfaces of different classes Bridge Separates an object’s interface from its implementation Composite A...

    java 反射简单用例

    java反射的用例代码 // //获取类对象的属性 // Field field = clazz.getDeclaredField("name");...// Object instance = constructor.newInstance(new Object[]{1, "李四", "中国"}); // System.out.println(instance);

    大数据关键技术与挑战

    大数据方面很不错的资料,分享给大家,在今后大数据、云的发展趋势下,希望大家有所收获

    Multiple Kernel Active Learning for Image Classification

    多核图像分类 Multiple Kernel Active Learning for Image Classification

    ant 简单教程

    一个ant的简单的教程,很适合基础的入门

    day021-反射和注解笔记和代码.rar

    Object instance = constructor.newInstance(Object 实参);//可以创建任意访问权限的有参或者无参构造 普通方法 Method[] methods = clazz.getMethods() 获取public修饰的构造方法数组,有父类中的...

    perl对象经典例题

    This example demonstrates how to a base class (or super class) inherited instance variables, and the instance variables to the new object in the method for their own constructor calls the base class.

    Cocoa Fundamentals Guide

    Instance and Class Methods 70 2010-12-13 | © 2010 Apple Inc. All Rights Reserved. 3 CONTENTS Object Retention and Disposal 70 How the Garbage Collector Works 71 How Memory Management Works 73 ...

Global site tag (gtag.js) - Google Analytics