java基础
java基础
类型转换
强制转换:高容量-->低容量
自动转换:低容量-->高容量
注意:不能对布尔值进行转换
字符串连接
int a=10,b=20;
//结果是1020,a,b都与前面的字符串进行拼接了
System.out.println(""+a+b);
//结果是30,a,b先进行运算
System.out.println(a+b+"");
Scanner对象
next():不能得到带有空格的字符串
nextLine():以Enter为结束符,可以得到输入回车之前的所有字符(包括空格)
方法重载
重载就是在一个类中,有相同的函数名称,但形参不同的函数。
数组
Arrays类
#打印数组元素
int[] a = new int[10];
Arrays.tostring(a)
#给数组中所有元素赋值为0
Arrays.fill(a,0)
稀疏数组
//需求:编写五子棋游戏中,有存盘退出和续上盘的功能
public class Main{
public static void main(String[] args){
//1.创建一个二维数组11*11 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 1;
//输出原始的数组
System.out.println("输出原始的数组");
for(int[] ints: array1){
for(int anInt: ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("====================");
//转换成稀疏数组保存,获取有效值的个数
int sum=0;
for(int i=0; i<11; i++){
for(int j=0; j<11; j++){
if(array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值的个数:"+sum);
//2.创建一个稀疏数组
int[][] array2=new int[sum+1][3];
//稀疏数组的第一行元素分别存放的是:行 列 值
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
//遍历二维数组,将非零的值,存放到稀疏数组中
int count=0;
for(int i=0; i<array1.length; i++){
for(int j=0; j<array1[i].length; j++){
if(array1[i][j]!=0){
count++;
array2[count][0]=i;
array2[count][1]=j;
array2[count][2]=array1[i][j];
}
}
}
//输出稀疏数组
System.out.println("稀疏数组");
for(int i=0; i<array2.length; i++){
System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t")
}
System.out.println("====================");
System.out.println("还原");
//1.读取稀疏数组
int[][] array3=new int[array2[0][0]][array2[0][1]];
//2.给其中的元素还原它的值
for(int i=1; i<array2.length; i++){
array3[array2[i][0]][array2[i][1]]=array2[i][2];
}
//3.打印
System.out.println("输出还原的数组");
for(int[] ints: array3){
for(int anInt: ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
面向对象
面向对象编程的本质就是:以类的方式组织代码,以对象(类的实例化)的方式封装数据。
三大特性:封装 继承 多态
构造器:1.和类名相同 2.没有返回值
作用:1.new的本质是调用构造方法 3.初始化对象的值
注意点:1.定义有参构造之后,如果想使用无参构造,必须得显示的定义一个无参的构造
Alt + Insert this. =
封装
属性私有,get/set
public class Student{
//属性私有
private String name;//名字
private int id;//学号
private char sex;//性别
//提供一些可以操作这个属性的方法!比如:public的get、set方法
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
}
public class Application{
public static void main(String[] args){
Student s1=new Student();
s1.setName("张三");
System.out.println(s1.getName());
}
}
继承
继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
注意:java中只有单继承,没有多继承。
object类 super 方法重写
public class Person{
public void say(){
System.out.println("说了一句话");
}
}
//学生也是人 子类继承父类,就会拥有父类的全部方法和属性(前提是修饰符都是public)
public class Student extends Person{
}
//老师也是人
public class Teacher extends Person{
}
public class Application{
public static void main(String[] args){
Student s1=new Student();
s1.say();
}
}
public class Person{
protected String name="张三";
public void print(){
System.out.println("Person");
}
public Person(){
System.out.println("Person无参执行了")
}
}
//学生也是人 子类继承父类,就会拥有父类的全部方法和属性(前提是修饰符都是public)
public class Student extends Person{
private String name="李四";
public void test(String name){
System.out.println(name);//输出传入的参数name
System.out.println(this.name);//输出自己的name
System.out.println(super.name);//输出父类的name
}
public void print(){
System.out.println("Student");
}
public void test1(){
print();//调用自己的print()方法
this.print();//调用自己的print()方法
super.print();//调用父类的print()方法
}
public Student(){
//隐藏代码:调用了父类的无参构造 super()
super()//调用父类的构造器,必须放在子类构造器的第一行
System.out.println("Student无参执行了")
}
}
public class Application{
public static void main(String[] args){
Student s1=new Student();
s1.test("王五");
s1.test1();
}
}
super注意点:
1. super调用父类的构造器,必须在构造方法的第一个
2. super必须出现在子类的方法或者构造方法中
3. super和this不能同时调用构造方法
Vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
前提
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法
this:本类的构造
super:父类的构造
public class A{
public static void test(){
System.out.println("A=>test()");
}
}
//重写都是方法的重写,和属性无关
public class B extends A{
public static void test(){
System.out.println("B=>test()");
}
}
public class Application{
public static void main(String[] args){
//方法的调用只和左边定义的数据类型有关
B b=new B();
b.test();//调用了B类的方法 输出B=>test()
//父类的引用指向了子类
A a=new B();
a.test();//调用了A类的方法 输出A=>test()
}
}
public class A{
public void test(){
System.out.println("A=>test()");
}
}
//重写都是方法的重写,和属性无关
public class B extends A{
//Override 重写
public void test(){
System.out.println("B=>test()");
}
}
/* 静态方法和非静态方法区别很大
静态方法:方法的调用只和左边定义的数据类型有关
非静态方法:重写
*/
public class Application{
public static void main(String[] args){
B b=new B();
b.test();//调用了B类的方法 输出B=>test()
//父类的引用指向了子类
A a=new B();//子类重写了父类的方法
a.test();//调用了B类的方法 输出B=>test()
}
}
重写:需要有继承关系,子类重写父类的方法!
1. 方法名必须相同
2. 参数列表必须相同
3. 修饰符:范围可以扩大但不能缩小。 public > Protected > Default > private
4. 抛出的异常:范围,可以缩小,但不能扩大。 ClassNotFoundException --> Exception(大)
重写,子类的方法和父类必须一致,方法体不同!
为什么需要重写:
1. 父类的功能,子类不一定需要,或者不一定满足!
Alt + Insert : override ;
多态
public class Person{
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
//重写父类方法
public void run(){
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
public class Application{
public static void main(String[] args){
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1=new Student();
//Person 父类型,可以指向子类,但是不能调用子类独有的方法
Person s2=new Student();
Object s3=new Student();
s2.run();//子类没重写父类方法之前输出了run; 重写父类方法之后输出了son
s1.run();//输出了son
s2.run();//报错。对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
s1.eat();//输出了eat
}
}
多态注意事项:
-
多态是方法的多态,属性没有多态
-
父类和子类有联系,当出现 ClassCastException 时,表示类型转换异常!
-
存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();
不能被重写的方法:
1. static方法,属于类,它不属于实例 2. final 常量; 3. private方法;
instanceof (类型转换) 引用类型
public class Person{
}
public class Student extends Person{
}
public class Teacher extends Person{
}
public class Application{
public static void main(String[] args){
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
//判断类型是否相似
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teache);//false
System.out.println(object instanceof String);//false
Person person = new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teache);//false
//System.out.println(person instanceof String);//编译报错!
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teache);//编译报错!
//System.out.println(person instanceof String);//编译报错!
}
}
注意点:
1. 父类引用指向子类的对象。
2. 把子类转换成父类,向上转型。
3. 把父类转换成子类,向下转型,强制转换。
4. 方便方法的调用,减少重复的代码。
static:
1. 实例化该类之后,才可以调用类里面的非静态方法
2. 静态方法可以直接被调用,或者通过类名. 来调用
public class Person{
//2:赋初值
{
System.out.println("匿名代码块");
}
//1:只执行一次
static{
System.out.println("静态代码块");
}
//3
public Person{
System.out.println("构造方法");
}
public static void main(String[] args){
Person person1=new Person();
/* 输出结果:
静态代码块
匿名代码块
构造方法
*/
}
}
抽象类
//抽象类 单继承
public abstract class Action{
//约束,有人帮我们实现
//abstract,抽象方法,只有方法名字,没有方法的实现!
public abstract void doSomething();
}
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法。除非它的子类也是抽象类
public class A extends Action{
public void doSomething(){
}
}
抽象类注意点:
1. 不能new这个抽象类,只能靠子类去实现它。
2. 抽象类中可以写普通的方法。
3. 抽象方法必须在抽象类中。
接口
接口只有规范,自己无法写方法。约束和实现分离:面向接口编程。
//interface 定义的关键字, 接口都需要有实现类
public interface UserService{
//接口中的所有变量其实都是固定的 public static final
//接口中的所有定义其实都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
public interface TimeService{
void timer();
}
//类可以实现接口 implements
//实现了接口的类,就需要重写接口中的方法 可以利用接口实现多继承
public class UserServiceImpl implements UserService, TimeService{
public void add(String name){
}
public void delete(String name){
}
public void update(String name){
}
public void query(String name){
}
public void timer(){
}
}
内部类
public class Outer{
private int id;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
}
public class Application{
public static void main(String[] args){
Outer outer=new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner=Outer.new Inner();
inner.getID();
}
}
异常处理机制
//假设要捕获多个异常:从小到大
try{//try监控区域
}catch(Error e){//catch(想要捕获的异常类型)捕获异常
System.out.println("Error");
}catch(Exception e){
System.out.println("Exception");
}catch(Throwable e){
System.out.println("Throwable");
}finally{//处理善后工作
System.out.println("finally");
}
浙公网安备 33010602011771号