zoukankan      html  css  js  c++  java
  • java类和对象

    类是对象的抽象 
    对象是类的一个实例
    类 对象 = new 类();
    拿对象可以操作这个类里的方法


          java类与对象的区别是个老生常谈的问题,刚开始学java的时候就开始接触类和对象,今天来总结一下他们之间的区别。

          首先讲讲class和object的区别,其实就是一句话:object是所有class的父类,所有class类都默认继承object。

          java中类是对一类“事物”的属性和行为一种抽象,比如人类就可以定义为一个Person类:

    public class Person {
    	public int age;//年龄
    	public String name;//姓名
    	public int hight;//身高
    	public int weight;//体重
    	public Person(){		
    	}
    	public Person(int age,String name,int hight,int weight){
    		this.age = age;
    		this.name = name;
    		this.hight = hight;
    		this.weight = weight;
    	}
    	public void doSth(){
    		//doSomething
    	}	
    }
    
            对象是类的实例化,也就是一个具体的个体,比如“摩罗”我就是Person类的一个具体的对象,我有自己具体的年龄姓名身高体重。

    简单来说类和对象的区别如下:

      1,类是一个抽象的概念,它不存在于现实中的时间/空间里,类只是为所有的对象定义了抽象的属性与行为。就好像“Person(人)”这个类,它虽然可以包含很多个体,但它本身不存在于现实世界上。
      2,对象是类的一个具体。它是一个实实在在存在的东西。
      3,类是一个静态的概念,类本身不携带任何数据。当没有为类创建任何对象时,类本身不存在于内存空间中。
      4,对象是一个动态的概念。每一个对象都存在着有别于其它对象的属于自己的独特的属性和行为。对象的属性可以随着它自己的行为而发生改变

    最后再来看看类和对象调用方法上的区别:

    public class classAndObject {
    	//静态方法
    	public static void staticTest (){
    		System.out.println("这是静态方法!");
    	}
    	//动态方法
    	public void dynamicTest() {
    		System.out.println("这是动态方法!");
    	}
    	public static void main(String[] args) {
    		classAndObject.staticTest();//正确
    		classAndObject t = new classAndObject();
    		t.dynamicTest();//非静态方法必须使用“对象.方法”,因为它在对象创建前不存在,必须依赖对象的创建才能使用。
    		t.staticTest(); //此处显示警告:The static method staticTest() from the type classAndObject should be accessed in a static way
            //静态方法在对象创建前就已经存在了,它的使用不依赖对象的创建,可以直接使用“类.静态方法”
  • 相关阅读:
    Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
    .net extjs 封装
    ext direct spring
    install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
    redis cookbook
    aptana eclipse plugin install on sts
    ubuntu open folderpath on terminal
    ubuntu install pae for the 32bit system 4g limited issue
    EXT Designer 正式版延长使用脚本
    用 Vagrant 快速建立開發環境
  • 原文地址:https://www.cnblogs.com/sanxinglan/p/4626608.html
Copyright © 2011-2022 走看看