9.java学习 20131212
2013-12-12 19:45
351 查看
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();
}
}
构造方法:
负责对对象的属性初始化
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();
}
}
构造方法:
负责对对象的属性初始化
相关文章推荐
- Java基础知识的三十个经典问答
- eclipse快捷键
- 工作分配问题 Java 回溯
- java去除List中重复的元素
- eclipse插件egit安装使用
- eclipse git 插件集成与本地使用(傻瓜教程 一)
- java中的synchronized理解
- 在web.xml中通过contextConfigLocation配置spring
- jdk的环境变量的配置
- java 泛型类型限定
- Spring AOP 详解
- java中String s=”abc“及String s=new String("abc")详解
- spring访问数据库
- Java补课之驯服Tiger:并发集合
- java -version的问题
- java Pattern Matcher
- Java基础知识的三十个经典问答
- 关于java中线程池的研究
- spring解析1
- Java自定义注解的定义与使用