Java:包装类,new初始化,去除复制后红线,装拆箱,解析转换类型,人类抽象类,接口

1、定义4种包装类型的变量,使用new关键字初始化这些变量。
package a;
public class fuxi {
public static void main(String[] args) {
Integer m1 = new Integer(5);
Boolean B1 = new Boolean(true);
Double D1 = new Double(52.0);
Byte Byte1 = new Byte("5");
Short Short1 = new Short("7");
Long Long1 = new Long("520");
Character Character1 = new Character('5');
Float Float1 = new Float(52.0);
System.out.print("Integer:" + m1 + " ,Boolean:" + B1 + ",Double:" + D1
+ ",Byte:" + Byte1 + ",Short:" + Short1 + ",Long:" + Long1
+ ",Character:" + Character1 + ",Float:" + Float1);
}
}














/*自己写和出的扩展题(记录自己的理解):使用vlueOf()方法将转换类型*/
package a;
public class fuxi {
public static void main(String[] args) {
Integer yuanlaideint = Integer.valueOf(100);
byte b = yuanlaideint.byteValue();
double dd = yuanlaideint.doubleValue();
System.out.println("原来的int类型 =" + yuanlaideint);
System.out.println("由原来的int类型变成的byte 类型=" + b);
System.out.println(" 由原来的int类型变成的double 类型" + dd);
Double yuanlaidedouble = Double.valueOf("123.45");
double d = yuanlaidedouble.doubleValue();
float f = yuanlaidedouble.floatValue();
int i = yuanlaidedouble.intValue();
long l = yuanlaidedouble.longValue();
System.out.println("原来的double类型=" + yuanlaidedouble);
System.out.println("由原来的double类型变成的double类型=" + d);
System.out.println("由原来的double类型变成的float类型 = " + f);
System.out.println("由原来的double类型变成的int类型 =" + i);
System.out.println("由原来的double类型变成的long类型 =" + l);
}
}








/*自己写和出的扩展题(记录自己的理解):使用vlueOf(),方法*/
package a;
public class fuxi {
public static void main(String[] args) {
String yuanlaidezifuchuan = "1111111";
int radix = 2;/* 二进制 */
// Creates an Integer object from the string从字符串创建一个整数对象
Integer intobject = Integer.valueOf(yuanlaidezifuchuan, radix);
// Extracts the int value from the string从字符串中提取int值
int intValue = Integer.parseInt(yuanlaidezifuchuan, 2);
System.out.println("原来的字符串 = " + yuanlaidezifuchuan);
System.out.println("由原来的字符串转变的2进制=" + intobject);
System.out.println(" 由原来的字符串解析的2进制=" + intValue);
}
}













// 3、定义4种包装数据类型的数组,每个数组的长度都是3。
// 对每种数组进行赋值,并打印。
package a;
public class fuxi {
public static void main(String[] args) {
Integer[] ms = new Integer[3];
Boolean[] B = new Boolean[3];
Double[] D = new Double[3];
Byte[] Byte = new Byte[3];
Short[] Short = new Short[3];
Long[] Long = new Long[3];
Character[] Character = new Character[3];
Float[] Float = new Float[3];
Integer m1 = new Integer(5);
Integer m2 = new Integer(2);
Integer m3 = new Integer(0);
Boolean B1 = new Boolean(true);
Boolean B2 = new Boolean(true);
Boolean B3 = new Boolean(true);
Double D1 = new Double(52.0);
Double D2 = new Double(52.0);
Double D3 = new Double(13.14);
Byte Byte1 = new Byte("5");
Byte Byte2 = new Byte("2");
Byte Byte3 = new Byte("0");
Short Short1 = new Short("7");
Short Short2 = new Short("7");
Short Short3 = new Short("5");
Long Long1 = new Long("520");
Long Long2 = new Long("13");
Long Long3 = new Long("14");
Character Character1 = new Character('5');
Character Character2 = new Character('2');
Character Character3 = new Character('0');
Float Float1 = new Float(52.0);
Float Float2 = new Float(52.0);
Float Float3 = new Float(13.14);
ms[0] = m1;
ms[1] = m2;
ms[2] = m3;
B[0] = B1;
B[1] = B2;
B[2] = B3;
D[0] = D1;
D[1] = D2;
D[2] = D3;
Byte[0] = Byte1;
Byte[1] = Byte2;
Byte[2] = Byte3;
Short[0] = Short1;
Short[1] = Short2;
Short[2] = Short3;
Long[0] = Long1;
Long[1] = Long2;
Long[2] = Long3;
Character[0] = Character1;
Character[1] = Character2;
Character[2] = Character3;
Float[0] = Float1;
Float[1] = Float2;
Float[2] = Float3;
for (int i = 0; i < ms.length; i++) {
System.out.print(ms[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(B[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(D[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(Byte[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(Short[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(Character[i] + " ");
}
System.out.println();
for (int i = 0; i < ms.length; i++) {
System.out.print(Float[i] + " ");
}
}
}









// 2、对4种包装类型使用valueOf方法和parseXxx方法
package a;
public class fuxi {
public static void main(String[] args) {
Integer yuanlaideInteger = 1;
String yuanlaidezifuchuan = "1111111";
// Boolean yuanlaideBoolean = null;
Double yuanlaideDouble = 1.1;
Byte yuanlaideByte = 1;
Short yuanlaideShort = 1;
Long yuanlaideLong = (long) 1;
Character yuanlaideCharacter = '1';
Float yuanlaideFloat = (float) 1;
int radix = 2;/* 二进制 */
Integer intobject = Integer.valueOf(yuanlaidezifuchuan, radix);
int intValue = Integer.parseInt(yuanlaidezifuchuan, 2);
System.out.println("原来的String = " + yuanlaidezifuchuan);
System.out.println("由原来的String转变的2进制Integer=" + intobject);
System.out.println(" 由原来的String解析的2进制int=" + intValue);
// Boolean yuanlaideBooleanv = Boolean.valueOf(yuanlaideBoolean);
//
// boolean yuanlaideBooleanp = Boolean.parseBoolean(yuanlaidezifuchuan);
// System.out.println("原来的Boolean = " + yuanlaideBoolean);
// System.out.println("由原来的Boolean 转变的Boolean=" + yuanlaideBooleanv);
// System.out.println(" 由原来的Boolean解析的boolean=" + yuanlaideBooleanp);
Integer yuanlaideDoublev = Integer.valueOf(16);
System.out.println("原来的Double = " + yuanlaideDouble);
System.out.println("由原来的Double 转变的16进制Integer=" + yuanlaideDoublev);
Integer yuanlaideIntegerv = Integer.valueOf(16);
System.out.println("原来的Integer = " + yuanlaideInteger);
System.out.println("由原来的Integer转变的16进制Integer=" + yuanlaideIntegerv);
Integer yuanlaideBytev = Integer.valueOf(yuanlaidezifuchuan, 16);
int yuanlaideBytep = Integer.parseInt(yuanlaidezifuchuan, 2);
System.out.println("原来的Byte = " + yuanlaideByte);
System.out.println("由原来的Byte 转变的16进制Integer=" + yuanlaideBytev);
System.out.println(" 由原来的Byte解析的2进制Integer=" + yuanlaideBytep);
Integer yuanlaideShortv = Integer.valueOf(7);
System.out.println("原来的Short = " + yuanlaideShort);
System.out.println("由原来的Short 转变的7进制Integer=" + yuanlaideShortv);
Integer yuanlaideLongv = Integer.valueOf(yuanlaidezifuchuan, 9);
int yuanlaideLongp = Integer.parseInt(yuanlaidezifuchuan, 2);
System.out.println("原来的Long = " + yuanlaideLong);
System.out.println("由原来的Long 转变的9进制Integer=" + yuanlaideLongv);
System.out.println(" 由原来的Long解析的2进制Integer=" + yuanlaideLongp);
Integer yuanlaideCharacterv = Integer.valueOf(8);
System.out.println("原来的Character = " + yuanlaideCharacter);
System.out
.println("由原来的Character 转变的8进制Integer=" + yuanlaideCharacterv);
Integer yuanlaideFloatv = Integer.valueOf(9);
System.out.println("原来的Float = " + yuanlaideFloat);
System.out.println("由原来的Float转变的9进制Integer=" + yuanlaideFloatv);
}
}




声明一个人类抽象类,人类都有吃饭和睡觉的功能,人类包含学生和老师,学生独有功能上课,老师独有功能讲课
学生张三独有的功能打篮球,学生李四独有功能踢足球
老师A独有功能看电视老师B独有功能钓鱼
老师和学生都8点上课(使用抽象类和接口做)
package a;
public class fuxi {
public static void main(String[] args) {
renlei A = new teacher("老师A");
renlei B = new teacher("老师B");
renlei Z = new student("张三");
renlei L = new student("李四");
renlei[] ms = new renlei[4];
ms[0] = A;
ms[1] = B;
ms[2] = Z;
ms[3] = L;
for (int i = 0; i < ms.length; i++) {
if (ms[i] instanceof teacher) {
if (i == 0) {
((teacher) ms[i]).jk();
((teacher) ms[i]).sj();
((teacher) ms[i]).kds();
} else if (i == 1) {
((teacher) ms[i]).jk();
((teacher) ms[i]).dy();
((teacher) ms[i]).sj();
}
}
else if (ms[i] instanceof student) {
if (i == 2) {
((student) ms[i]).sk();
((student) ms[i]).sj();
((student) ms[i]).dlq();
} else if (i == 3) {
((student) ms[i]).sk();
((student) ms[i]).sj();
((student) ms[i]).tzq();
}
}
}
/* 向上转型,用上抽象类。向上转型,用上接口 */
}
}
interface jiangke {
public abstract void bk();
void jk();
}
abstract class renlei {
int age;
String id;
String name;
public abstract void chifan();
public abstract void shuijiao();
}
interface shangke {
public abstract void sk();
}
class student extends renlei implements shangke {
public student(String name) {
this.name = name;
}
@Override
public void chifan() {
System.out.println(this.name + "吃饭");
}
public void dlq() {
System.out.println(this.name + "打篮球");
}
@Override
public void shuijiao() {
System.out.println(this.name + "睡觉");
}
public void sj() {
System.out.println(this.name + "8点上课");
}
@Override
public void sk() {
System.out.println(this.name + "在上课");
}
public void tzq() {
System.out.println(this.name + "踢足球");
}
}
class teacher extends renlei implements jiangke {
public teacher(String name) {
this.name = name;
}
@Override
public void bk() {
// TODO Auto-generated method stub
}
@Override
public void chifan() {
System.out.println(this.name + "在吃饭");
}
public void dy() {
System.out.println(this.name + "钓鱼");
}
@Override
public void jk() {
System.out.println(this.name + "在讲课");
}
public void kds() {
System.out.println(this.name + "看电视");
}
@Override
public void shuijiao() {
System.out.println(this.name + "睡觉");
}
public void sj() {
System.out.println(this.name + "8点上课");
}
}








