黑马学习笔记--关于构造函数
来源:互联网 发布:mac玩lol国服 编辑:程序博客网 时间:2024/06/05 08:52
中英文切换有点麻烦,为了让编程效率更快,决定练习使用英文注释,完成整个程序后,在添加中文注释。这样既免去了中英文切换的麻烦(实际上非常的麻烦!),还可以提高英文水平,熟悉专业术语。
本期遇到的问题是:1.如何保证不会将一个浮点数据输入给age呢?即禁止默认的类型转化,未解决
2.我可以指定null到age吗?实验证明不行(提示需要int,找到空型) 解决:String类默认是NULL,可以将NULL赋给String,int ,char,float,byte等默认值为0,boolean默认为flase
3.类名可以和其他类的函数名相同吗?(我把类名改成compare后,程序正常运行)
command:define a class of student,it includes such elements:name, age 定义一个学生类,包括名字,年龄
and interfaces(ports) to use and change the elements;还需要一些接口来访问私有成员变量
it also includes some simple methods(for exemple:comparing age with the other objects);包括一个比较年龄的简单的成员方法
to pratice the construct function,I also need different constructs;为了练习构造函数,我需要不止一个构造函数
PS:the clsss may become bigger,so I need make sure the whole programmer is strong;
*/
class Student //TO M:the class name should be upper case 提醒自己,类名首写字母必须是大写
{
private String name;
private int age;//how to make sure that someone won`t input a float into the 'age' 问题,如何保证不会将一个浮点数据输入给age呢?即禁止默认的类型转化
Student()
{
//this.name=null;
//this.age=null;//can I assign NULL to a int? 我可以指定null到age吗?实验证明不行(提示需要int,找到空型)
System.out.println("objects A created sucessed");
}
Student(String name)
{
this.name=name;
//this.age=null;
System.out.println("objects B created sucessed");
}
Student(int age)
{
//this.name=null;
this.age=age;
System.out.println("objects C created sucessed");
}
Student(String name, int age)
{
this.name=name;
this.age=age;
System.out.println("objects D created sucessed");
}
void setName(String name)
{
this.name=name;
}
String getname()
{
return this.name;
}
boolean compare (Student b)
{
if (this.age>b.age)
{
System.out.println(this.name+"is biger");
return true;
}
else
{
System.out.println(this.name+"is smaller");
return false;
}
}
void show ()
{
System.out.println("name:"+name+'\n'+"age="+age);//can it run?转义字符就是这么用的
}
}
class compareStudent//can the class name be the same with other class's function name?类名可以和其他类的函数名相同吗?(我把类名改成compare后,程序正常运行)
{
public static void main(String[] args)
{
Student lily=new Student("lily",20);
Student lilei= new Student("lilei",22);
lily.show();
lilei.show();
lily.compare(lilei);
Student lisa=new Student("lisa",'a');//i input a char,and the output "age" is 97,it's not right!back to line 19 输入char类型的会自动转化成int型,如何避免?
lisa.show();
/*Student hanmeimei=new Student("hanmeimei",20.1);
hanmeimei.show();*/
}
}
- 黑马学习笔记--关于构造函数
- 黑马程序员之C#学习笔记:关于C#构造函数
- 黑马程序员--构造函数的学习笔记
- 《黑马程序员》java笔记->【02】关于封装,构造函数
- 黑马程序员--学习笔记--对构造函数的认识
- 黑马程序员_学习笔记07-构造函数
- 黑马程序员_学习笔记04 构造函数
- 黑马程序员----关于构造函数与的学习以及this指针在构造函数间的调用规则
- 黑马程序员----------java构造函数笔记
- 黑马学习---------------反射(构造函数)
- 拷贝构造函数学习笔记
- C++构造函数学习笔记
- 构造函数学习笔记(1)
- 学习笔记:构造函数转发
- JS构造函数学习笔记
- OC学习笔记,构造函数
- 学习笔记01-构造函数
- js构造函数学习笔记
- 学习笔记之Runtime类
- nbu 2415 Zero打怪兽
- Aop javacript 实现
- HTML5之FileReader的使用
- 简记c、c++、java异常处理机制的区别
- 黑马学习笔记--关于构造函数
- dll动态加载的方式
- TCP三次握手
- uva10048 - Audiophobia()
- 数据挖掘(五):聚类
- VS2010拖Web用户控件时卡死 解决方案
- (4M+2)阶魔方阵算法
- [Oracle] Oracle 入门命令
- stack的数组实现