即使今天是我生日,也要认真学习。。。
包就是将类放在某个文件夹中方便管理和分类。
包中类的执行
package mypack.a;
public class Person //因为要外部访问,所以权限应该为public
{
public void talk(){ //内部函数的权限也应该是public
System.out.println("讲话");
}
public static void main(String[] args)
{
Person p=new Person();
p.talk();
}
}
执行时应将编译出的Person类放在mypack文件夹下的a文件夹下
包中类的调用
import mypack.a.Person;
class PackDemo{
public static void main(String[] args)
{
Person p=new Person();
p.talk();
}
}
权限修饰符
object类
构造方法
包装类
Java中基本类型功能简单,不具备对象的特征,为了使基本类型具有对象的特征,所以有了包装类
基本数据类型与字符串之间进行转换
class WrapperDemo{
public static void main(String[] args)
{
public static void main(String[] args)
{
/*基本数据类型类型转换成字符串方法:
1.将i+""
2.String类中静态方法valueOf(基本数据类型)
*/
int i1=3;
String s=i1+"";
String x=String.valueOf(i1);
/*字符串转换成基本数据类型
1.使用包装类中的静态方法Integer.parseInt("123")
int parseInt()
long ParseLong()
boolean ParseBoolean()
只有character没有Parse方法
2.如果字符串被Integer封装成一个对象
可以调用intValue()
*/
String str="123";
int i2=Integer.parseInt(str);
Integer i3=new Integer("1234");
int y=i3.intValue();
System.out.println(i3);
System.out.println(i2);
}
}
基本数据类型与包装类之间进行转换
class WrapperDemo1{
public static void main(String[] args)
{
/*基本数据类型-->包装类
1.通过构造函数来完成
2.自动装箱
*/
int i=1;
Integer x=new Integer(i);
Integer y=3; //自动装箱
/*包装类-->基本数据类型
1.通过 intValue()
2.自动拆箱
*/
int a=y.intValue();
int b=y; //自动拆箱
System.out.println(a);
System.out.println(b);
}
}