9.java学习 20131212

来源:互联网 发布:jquery与javascript 编辑:程序博客网 时间:2024/05/17 02:00


2013/112/12 

9.java学习

Java>Resert

栈 ——里分配的内存空间,不需要程序员过问,有系统自动管理

堆 —— 里存放的数据,需要程序员手工干预

 

所有的值类型及局部变量都是在栈中被分配的内存空间

所有的引用类型的数据保存堆中

 

(从语义上理解:是用于创建对象的模版,是一个抽象的概念

  从语法上理解:类是我们特定的一种数据类型)

特征(属性)  和 行为(方法)

具有相同属性和方法的一组对象的集合

 

类是对象的类型

不同于int 类型:具有方法

 

 

类与对象的关系

从语义上理解:

类是创建对象的模版,而对象是类的一个实例

从语法上理解:

类就是我们定义的一种数据类型,而对象就是这种数据类型的变量

newpackage包下定义类OtherTest

package newpackage;

 

public class OtherTest {

//创建一个属性

public String shcoolName;

//创建一个方法

public void setSchoolName(String schoolName)

{

//this 指针的用法

this.shcoolName=schoolName;

}

//输出信息

    public  void showInfo()

    {

    System.out.println("毕业学校:"+this.shcoolName);

    }

}

 

在包com.test.schoo下定义类Test引用以上类:

 

package com.test.school;

import newpackage.OtherTest;

 

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

OtherTest othTest=new OtherTest();

othTest.setSchoolName("清华大学");

othTest.showInfo();

}

}

 

 

package newpackage;

 

import java.util.Scanner;

 

public class VisitorPrice {

 

private String name;//姓名

private int age;//年龄

public void showPrice()

{

Scanner input=new Scanner(System.in);

System.out.print("请输入姓名:");

name=input.next();

System.out.print("请输入年龄:");

this.age=input.nextInt();

if(this.age<68&&this.age>18)

{

System.out.println(this.name+",您好,您的门票是"+"20元");

}

else

{

System.out.println(this.name+"您的门票免费");

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

VisitorPrice visPre=new VisitorPrice();

visPre.showPrice();

 

}

}

 

构造方法:

负责对对象的属性初始化

 

 

 

 

 

 

 

 

0 0
原创粉丝点击