1.对象初始化
当我们new了一个对象出来的时候,成员变量在初始化的时候,java会自动给它一个0值。
如果你是一个boolean=false 如果是String=null int和double都是0。
并且可以在创建成员变量的时候给它赋值
int price =50; 而不能 int price; price=50;
2 构造函数(创造对象的时候,先进行成员变量的初始化后,所执行的函数)(构造方法)
这个函数是没有返回类型的。void也不行
如果没有编写构造方法,系统会自动赠送一个无参数构造方法,构造方法是通过 new 这个关键字来创建,并且构造方法没有返回值。
public class VendingMechine {
VendingMechine() //构造函数
{
total=0;
}
}

并且可以有两个构造函数
public class VendingMechine {
int price=80;
int total;
int balance;
VendingMechine(int price) //构造函数1
{
this.price=price;
}
VendingMechine() //构造函数2
{
total=0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//做动作
VendingMechine a=new VendingMechine();
VendingMechine b=new VendingMechine(100);
}
}

函数重载是在有多个构造方法的情况下,在创建对象是根据参数类型来调对应的构造方法。
1.在创建对象的时候,给了int值,则调用有int值的那个构造函数,给double就调用有doublez值的那个构造函数。
2.问题:
在零基础学Java中曾经提到过一个类型自动转换的事情。就是如果一个地方,比如函数的参数需要的是比较宽的类型的数据,比如double就比int来得宽,那么如果调用函数的时候给出了int值,是会被自动转换成double去调用函数的。
现在,如果有两个函数重载,一个是double,一个是int,还会发生类型自动转换吗?
不会,java会根据int 和 double类型来调用相应的构造函数
import java.util.Scanner;
public class VendingMechine {
int price=80;
double total=50;
int balance;
VendingMechine(int price)
{
System.out.println(price);
}
VendingMechine(double total)
{
System.out.println(total);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
VendingMechine a=new VendingMechine(10);
VendingMechine b=new VendingMechine((Math.random()*100));
}
}
3.还有一个写法,就是通过 this() 在有参数的构造函数里面调用那个没有参数的构造函数
并且只能在构造函数里的第一句出现 只能使用一次;
public class VendingMechine {
int price=80;
int total;
int balance;
VendingMechine(int price) //构造函数1
{ this();
this.price=price;
}
VendingMechine() //构造函数2
{
total=0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
VendingMechine a=new VendingMechine();
VendingMechine b=new VendingMechine(100);
}
}
第一周编程题:
import java.util.Scanner;
/*class Fraction{
int nume; //成员变量:分子。
int deno; //成员变量:分母。
Fraction(int nume, int deno)
{
this.nume = nume;
this.deno = deno;
}
double toDouble() //将分数转换为double。
{
return (double)(nume/deno);
}
Fraction plus(Fraction r) //将自己的分数和r的分数相加,产生一个新的Fraction的对象。
{
Fraction ret = new Fraction(0,1);
ret.nume = nume*r.deno+deno*r.nume;
ret.deno = deno*r.deno;
return ret;
}
Fraction multiply(Fraction r) //将自己的分数和r的分数相乘,产生一个新的Fraction的对象。
{
Fraction ret = new Fraction(0,1);
ret.nume = nume*r.nume;
ret.deno = deno*r.deno;
return ret;
}
void print() //将自己以“分子/分母”的形式输出到标准输出,并带有回车换行。
{
int temp;
int nume = this.nume;
int deno = this.deno;
while( deno!=0 ) //求分子分母的最大公约数。
{
temp = nume%deno;
nume = deno;
deno = temp;
}
this.nume/=nume; //约分过程。
this.deno/=nume;
if( this.nume==this.deno )
{
System.out.println(1);
}
else
{
System.out.println(this.nume+"/"+this.deno);
}
}
}*/
class Fraction
{
int fenzi;
int fenmu;
double result;
double math1,math2;
//构造函数
Fraction(int fenzi,int fenmu)
{
this.fenmu=fenmu;
this.fenzi=fenzi;
}
//将分数转换为double
double toDouble()
{
result=(double)(this.fenzi/this.fenmu);
return result;
}
//将自己的分数和r的分数相加,产生一个新的Fraction的对象。注意小学四年级学过两个分数如何相加的哈。
Fraction plus(Fraction r)
{
Fraction a=new Fraction(0,1);
a.math1= ((a.fenzi*r.fenmu)/(a.fenmu*r.fenmu));
a.math2= ((r.fenzi*a.fenmu)/(r.fenmu*a.fenmu));
a.math1+=a.math2;
return a;
//为啥return a
}
//将自己的分数和r的分数相乘,产生一个新的Fraction的对象
Fraction multiply(Fraction r)
{
Fraction b=new Fraction(0,1);
b.math1=r.fenmu*b.fenmu;
b.math2=r.fenzi*b.fenzi;
b.math1*=b.math2;
return b;
}
//将自己以“分子/分母”的形式输出到标准输出,并带有回车换行。如果分数是1/1,应该输出1。当分子大于分母时,不需要提出整数部分,即31/30是一个正确的输出。
void print()
{
System.out.println(this.fenzi);
System.out.println(this.fenmu);
while(this.fenmu!=0)
{
if(this.fenmu==this.fenzi)
{
System.out.println(1);
break;
}
else
{
fenzi=this.fenzi;
fenmu=this.fenmu;
if(this.fenzi!=0)
{
this.fenzi/=this.fenzi;
this.fenmu/=this.fenzi;
}
else
{
System.out.println(this.fenzi+"/"+this.fenmu);
}
}
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Fraction a = new Fraction(in.nextInt(),in.nextInt());
Fraction b = new Fraction(in.nextInt(),in.nextInt());
a.print();
b.print();
a.plus(b);
a.multiply(b).plus(new Fraction(5,6)).print(); ////
a.print();
b.print();
in.close();
}
}
3.private关键字的使用
3.1 一旦使用了private方法,就只能在本类访问 ,在其他类不能直接访问,可以间接访问
3.2 间接访问的方法:用 setXxx和 getXxx ,并且setXxx不能有返回值,参数类型与成员变量要对应,对于getXxx来说,不能有参数,返回值类型与成员变量对应。
3.3 但是对于基本数据类型boolean来说,getXxx要写成isXxx的形式,而setXxx规则不变。
下面是代码
public class gamer {
private String gm="赛博朋克2077";
private int price=299;
private boolean buyit;
public void setPrice(int a)
{
price=a;
}
public int getPrice()
{
return price;
}
public void setGm(String a)
{
gm=a;
}
public String getGm()
{
return gm;
}
public void setBuyit(boolean q)
{
buyit=q;
}
public boolean isBuyit()
{
return buyit;
}
void print(boolean a)
{
System.out.println(a);
}
void print(String a)
{
System.out.println(a);
}
void print(int a)
{
System.out.println(a);
}
}
//已经在game 写了setter和getter了
public class gamer1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
gamer a=new gamer();
System.out.println();
a.setPrice(5);
a.getPrice();
a.setBuyit(true);
a.setGm("2077牛逼");
a.getGm();
a.print(a.getGm());
a.print(a.getPrice());
a.print(a.isBuyit());
//2077牛逼
//5
//false
}
}
4.封装性
封装就是将一些东西隐藏起来(相对于main方法来说)。
在main方法调用方法的时候,main方法不会管成员方法里面是怎么写的,只看结果。就像你老板一样,不看过程,只看结果
代码
package homework;
import java.util.Scanner;
public class homework1 {
public int getmax(int array[])
{ int max=array[0];
for(int i=0;i<array.length;i++)
{
if(array[i]>max)
{
max=array[i];
}
}
return max;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[ ]array=new int[5];
for(int i=0;i<array.length;i++)
{
array[i]=(int)(Math.random()*100);
}
homework1 a=new homework1();
System.out.print(a.getmax(array));
}
}
这篇博客探讨了Java中的对象初始化,说明了成员变量如何被自动赋予默认值,以及如何在创建时初始化。文章还深入介绍了构造函数,包括它们的作用、系统如何提供默认构造器以及函数重载的概念。作者提出一个问题,当存在类型重载时,Java是否会自动转换类型来调用构造函数,答案是不会,Java会根据实际参数类型选择合适的构造函数。此外,博客还讲解了`this()`关键字的使用,允许在有参数的构造函数中调用无参数构造函数。最后,博主介绍了`private`关键字的作用,强调了封装在Java中的重要性,以及如何通过setter和getter方法实现封装。


被折叠的 条评论
为什么被折叠?



