java构造方法基础入门.docx
- 文档编号:27625058
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:12
- 大小:66.36KB
java构造方法基础入门.docx
《java构造方法基础入门.docx》由会员分享,可在线阅读,更多相关《java构造方法基础入门.docx(12页珍藏版)》请在冰豆网上搜索。
java构造方法基础入门
1构造方法
/*
构造方法:
给对象的数据进行初始化
格式:
A:
方法名与类名相同
B:
没有返回值类型,连void都没有
C:
没有具体的返回值
*/
classStudent{
privateStringname;//null
privateintage;//0
publicStudent(){
System.out.println("这是构造方法");
}
}
classConstructDemo{
publicstaticvoidmain(String[]args){
//创建对象
Students=newStudent();
System.out.println(s);//Student@e5bbd6
}
}
2给成员变量赋值
/*
我们一直在使用构造方法,但是,我们确没有定义构造方法,用的是哪里来的呢?
构造方法的注意事项:
A:
如果我们没有给出构造方法,系统将自动提供一个无参构造方法。
B:
如果我们给出了构造方法,系统将不再提供默认的无参构造方法。
注意:
这个时候,如果我们还想使用无参构造方法,就必须自己给出。
建议永远自己给出无参构造方法
给成员变量赋值有两种方式:
A:
setXxx()
B:
构造方法
*/
classStudent{
privateStringname;
privateintage;
publicStudent(){
//System.out.println("我给了,你还给不");
System.out.println("这是无参构造方法");
}
//构造方法的重载格式
publicStudent(Stringname){
System.out.println("这是带一个String类型的构造方法");
this.name=name;
}
publicStudent(intage){
System.out.println("这是带一个int类型的构造方法");
this.age=age;
}
publicStudent(Stringname,intage){
System.out.println("这是一个带多个参数的构造方法");
this.name=name;
this.age=age;
}
publicvoidshow(){
System.out.println(name+"---"+age);
}
}
classConstructDemo2{
publicstaticvoidmain(String[]args){
//创建对象
Students=newStudent();
s.show();
System.out.println("-------------");
//创建对象2
Students2=newStudent("林青霞");
s2.show();
System.out.println("-------------");
//创建对象3
Students3=newStudent(27);
s3.show();
System.out.println("-------------");
//创建对象4
Students4=newStudent("林青霞",27);
s4.show();
}
}
3手机类的成员方法
/*
标准的手机类练习
手机类:
成员变量:
brand,price,color
构造方法:
无参构造
成员方法:
getXxx()/setXxx()
*/
//定义手机类
classPhone{
//品牌
privateStringbrand;
//价格
privateintprice;
//颜色
privateStringcolor;
//无参构造方法
publicPhone(){}
//getXxx()和setXxx()方法
publicStringgetBrand(){
returnbrand;
}
publicvoidsetBrand(Stringbrand){
this.brand=brand;
}
publicintgetPrice(){
returnprice;
}
publicvoidsetPrice(intprice){
this.price=price;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
}
//手机测试类
classPhoneTest{
publicstaticvoidmain(String[]args){
//创建对象
Phonep=newPhone();
//给成员变量赋值
p.setBrand("诺基亚");
p.setPrice(199);
p.setColor("土豪金");
//获取值
System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());
}
}
4类的组成
/*
类的组成:
成员变量,成员方法
今天我们又加入了一个新的成员:
构造方法。
以后再提类的组成:
成员变量
构造方法
成员方法
根据返回值:
void类型
非void类型
形式参数:
空参方法
非空参方法
*/
classStudent{
publicStringgetString(){
return"helloworld";
}
publicvoidshow(){
System.out.println("show");
}
publicvoidmethod(Stringname){
System.out.println(name);
}
publicStringfunction(Strings1,Strings2){
returns1+s2;
}
}
classStudentDemo{
publicstaticvoidmain(String[]args){
//创建对象
Students=newStudent();
//调用无参无返回值方法
s.show();
//调用无参有返回值方法
Stringresult=s.getString();
System.out.println(result);
//调用带参无返回值的方法
s.method("林青霞");
//调用带参带返回值的方法
Stringresult2=s.function("hello","world");
System.out.println(result2);
}
}
5标准类
/*
一个标准代码的最终版。
学生类:
成员变量:
name,age
构造方法:
无参,带两个参
成员方法:
getXxx()/setXxx()
show():
输出该类的所有成员变量值
给成员变量赋值:
A:
setXxx()方法
B:
构造方法
输出成员变量值的方式:
A:
通过getXxx()分别获取然后拼接
B:
通过调用show()方法搞定
*/
classStudent{
//姓名
privateStringname;
//年龄
privateintage;
//构造方法
publicStudent(){
}
publicStudent(Stringname,intage){
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
//输出所有的成员变量值
publicvoidshow(){
System.out.println(name+"---"+age);
}
}
//测试类
classStudentTest{
publicstaticvoidmain(String[]args){
//方式1给成员变量赋值
//无参构造+setXxx()
Students1=newStudent();
s1.setName("林青霞");
s1.setAge(27);
//输出值
System.out.println(s1.getName()+"---"+s1.getAge());
s1.show();
System.out.println("----------------------------");
//方式2给成员变量赋值
Students2=newStudent("刘意",30);
System.out.println(s2.getName()+"---"+s2.getAge());
s2.show();
}
}
6创建对象都做了那些事情
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 构造 方法 基础 入门