JAVA-零碎知识点
你若要喜爱你自己的价值,你就得给世界创造价值。——歌德
函数的重载
- 什么时候用重载?
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。
/*
void show(int a,char b,double c){}
a.
void show(int x,char y,double z){}//没有,因为和原函数一样。
b.
int show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。
c.
void show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。
d.
boolean show(int c,char b){}//重载了,因为参数个数不同。
e.
void show(double c){}//重载了,因为参数个数不同。
f.
double show(int x,char y,double z){}//没有,这个函数不可以和给定函数同时存在与一个类中。
*/
创建数组的三种方式
int vec[] = new int[]{1, 5, 3}; // 第一种方法
int vec[] = { 37 , 47 , 23 } ; // 第二种方法
int vec[] = new int [3];
for(int i=0;i<3;i++)
vec[i]=i+1; //第三种方法
关于选择排序和冒泡排序
选择排序法
class ArrayTest2
{
/*
选择排序。
内循环结束一次,最值出现头角标位置上。
*/
public static void selectSort(int[] arr)
{
for (int x=0; x<arr.length-1 ; x++)
{
for(int y=x+1; y<arr.length; y++)
{
if(arr[x]>arr[y])
{
/*
int temp = arr[x];
arr[x] = arr[y];
arr[y]= temp;
*/
swap(arr,x,y);
}
}
}
}
冒泡排序法
public static void bubbleSort(int[] arr)
{
for(int x=0; x<arr.length-1; x++)
{
for(int y=0; y<arr.length-x-1; y++)//-x:让每一次比较的元素减少,-1:避免角标越界。
{
if(arr[y]<arr[y+1])
{
/*
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
*/
swap(arr,y,y+1);
}
}
}
}
选择排序法实际上是保证了每次循环的第一个是最小,而冒泡排序法则保证了每次循环的最后一个是最大。
二维数组
/*
int[] x; int x[];
int[][] y; int y[][]; int[] y[];
int[] x,y[];//x一维,y二维。
int[] x;
int[] y[];
a.
x[0] = y;//error
b.
y[0] = x;//yes
c.
y[0][0] = x;//error
d.
x[0][0] = y;//error
e.
y[0][0] = x[0];//yes
f.
x=y;//error
*/
构造函数当中this 的应用
/*
this语句 :用于构造函数之间进行互相调用。
this语句只能定义在构造函数的第一行。因为初始化要先执行。
*/
class Person
{
private String name;
private int age;
{
System.out.println("code run");
}
Person()
{
//this("hah");
System.out.println("person run");
}
Person(String name)
{
//this();
this.name =name;
}
Person(String name,int age)
{
//this(name);
//this.name = name;
this.age = age;
}
}
class PersonDemo4
{
public static void main(String[] args)
{
new Person();
//Person p = new Person("lisi",30);
//Person p1 = new Person("lisi2",36);
}
}
关于子父类的构造函数
/*
子父类中的构造函数。
在对子类对象进行初始化时,父类的构造函数也会运行,
那是因为子类的构造函数默认第一行有一条隐式的语句 super();
super():会访问父类中空参数的构造函数。而且子类中所有的构造函数默认第一行都是super();
为什么子类一定要访问父类中的构造函数。
因为父类中的数据子类可以直接获取。所以子类对象在建立时,需要先查看父类是如何对这些数据进行初始化的。
所以子类在对象初始化时,要先访问一下父类中的构造函数。
如果要访问父类中指定的构造函数,可以通过手动定义super语句的方式来指定。
注意:super语句一定定义在子类构造函数的第一行。
子类的实例化过程。
结论:
子类的所有的构造函数,默认都会访问父类中空参数的构造函数。
因为子类每一个构造函数内的第一行都有一句隐式super();
当父类中没有空参数的构造函数时,子类必须手动通过super语句形式来指定要访问父类中的构造函数。
当然:子类的构造函数第一行也可以手动指定this语句来访问本类中的构造函数。
子类中至少会有一个构造函数会访问父类中的构造函数。
*/
class Fu //extends Object
{
int num ;
Fu()
{
//super();
num= 60;
System.out.println("fu run");
}
Fu(int x)
{
System.out.println("fu ...."+x);
}
}
class Zi extends Fu
{
Zi()
{
super();
//super(4);
System.out.println("zi run");
}
Zi(int x)
{
this();
//super();
//super(3);
System.out.println("zi..."+x);
}
}
class ExtendsDemo4
{
public static void main(String[] args)
{
Zi z = new Zi(0);
System.out.println(z.num);
}
}
关于多态
关于多态中的成员变量引用的规则
class Fu
{
static int num = 5;
void method1()
{
System.out.println("fu method_1");
}
void method2()
{
System.out.println("fu method_2");
}
static void method4()
{
System.out.println("fu method_4");
}
}
class Zi extends Fu
{
static int num = 8;
void method1()
{
System.out.println("zi method_1");
}
void method3()
{
System.out.println("zi method_3");
}
static void method4()
{
System.out.println("zi method_4");
}
}
class DuoTaiDemo4
{
public static void main(String[] args)
{
// Fu f = new Zi();
//
// System.out.println(f.num);
//
// Zi z = new Zi();
// System.out.println(z.num);
//f.method1();
//f.method2();
//f.method3();
Fu f = new Zi();
System.out.println(f.num);
f.method4();
Zi z = new Zi();
z.method4();
/*
在多态中成员函数的特点:
在编译时期:参阅引用型变量所属的类中是否有调用的方法。如果有,编译通过,如果没有编译失败。
在运行时期:参阅对象所属的类中是否有调用的方法。
简单总结就是:成员函数在多态调用时,编译看左边,运行看右边。
在多态中,成员变量的特点:
无论编译和运行,都参考左边(引用型变量所属的类)。
在多态中,静态成员函数的特点:
无论编译和运行,都参考做左边。
*/
// Zi z = new Zi();
// z.method1();
// z.method2();
// z.method3();
}
}
5
fu method_4
zi method_4
借口行引用指向自己的子类对象
interface PCI
{
public void open();
public void close();
}
class MainBoard
{
public void run()
{
System.out.println("mainboard run ");
}
public void usePCI(PCI p)//PCI p = new NetCard()//接口型引用指向自己的子类对象。
{
if(p!=null)
{
p.open();
p.close();
}
}
}
class NetCard implements PCI
{
public void open()
{
System.out.println("netcard open");
}
public void close()
{
System.out.println("netcard close");
method();
}
}
class SoundCard implements PCI
{
public void open()
{
System.out.println("SoundCard open");
}
public void close()
{
System.out.println("SoundCard close");
}
}
/*
class MainBoard
{
public void run()
{
System.out.println("mainboard run");
}
public void useNetCard(NetCard c)
{
c.open();
c.close();
}
}
class NetCard
{
public void open()
{
System.out.println("netcard open");
}
public void close()
{
System.out.println("netcard close");
}
}
*/
class DuoTaiDemo5
{
public static void main(String[] args)
{
MainBoard mb = new MainBoard();
mb.run();
mb.usePCI(null);
mb.usePCI(new NetCard());
mb.usePCI(new SoundCard());
}
}