JAVA学习04
java方法
- 方法:一个方法只完成1个功能
public class Demo01 {
public static void main(String[] args) {
int max = max(10,10);
System.out.println(max);
}
public static int max(int a,int b){
int result=0;
if (a==b){
System.out.println("两数相等");
return 0;
}
if (a>b){
result = a;
}else {
result = b;
}
return result;
}
}
- 方法重载:一个类中,相同的函数名字,但形参不同
public class Demo01 {
public static void main(String[] args) {
double max = max(10.1,10.2);
System.out.println(max);
}
public static int max(int a,int b){
int result=0;
System.out.println("方法int");
if (a==b){
System.out.println("两数相等");
return 0;
}
if (a>b){
result = a;
}else {
result = b;
}
return result;
}
public static double max(double a,double b){
double result=0;
System.out.println("方法float");
if (a==b){
System.out.println("两数相等");
return 0;
}
if (a>b){
result = a;
}else {
result = b;
}
return result;
}
}
自己写的计算器小demo
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请写出两个数字,用空格隔开");
double a = scan.nextDouble();
double b = scan.nextDouble();
System.out.println("填写序列号:1.加2.减,3.乘4.除");
int c = scan.nextInt();
double d = 0;
switch (c) {
case 1:
d = a + b;
break;
case 2:
d = a - b;
break;
case 3:
d = a * b;
break;
case 4:
d = a / b;
break;
}
System.out.println("结果为:" + d);
}
}
}
java数组
数组:就是一个team,数组对象本身在堆中,一旦创建,不可改变,元素类型相同
public class Demo01 {
public static void main(String[] args) {
int[] nums;//声明数组
nums = new int[10];//创建数组
//一步:int[] nums =new int[10];
//赋值
nums[0]=1;
nums[1]=2;
nums[2]=3;
nums[3]=4;
nums[4]=5;
nums[5]=6;
nums[6]=7;
nums[7]=8;
nums[8]=9;
nums[9]=10;
int sum = 0;
//求数组的总和
for(int i=0;i<nums.length;i++){
sum = sum+nums[i];
}
System.out.println(sum);
}
}
冒泡排序
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
int[] arr = {10,23,44,55};
int[] sort = sort(arr);
System.out.println(Arrays.toString(sort));
System.out.println(sort[2]);
}
public static int[] sort(int[] nums) {
int temp=0;
for (int i = 0;i<nums.length-1;i++){
for (int j=0;j<nums.length-i-1;j++){
if (nums[j+1]<nums[j]){
temp = nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
return nums;
}
}
java面向对象编程
java的核心思想就是oop
-
面向对象与面向过程:面向对象就是物以类聚,分类的思维模式,面向过程就是步骤简单,分一步一步来。
本质:以类的方式组织代码,以对象的组织(封装)数据。
特性:封装,继承,多态
-
public class Demo01 { public static void main(String[] args) { /* 类:抽象的,实例化 类实例化后会返回一个自己的对象 animal对象就是一个Animal类的具体实例 */ Animal animal = new Animal(); animal.name="旺财"; animal.run(); System.out.println(animal.name); animal.name="siri"; animal.run(); System.out.println(animal.name); } } ===============================================public class Animal { String name; int age; public void run(){ System.out.println(name+"在跑"); } } -
创建与初始化对象:使用new关键字创建对象,它除了分配内存空间,还会给创建好的对象进行默认的初始化以及对类中的构造器的调用
类中的构造器也称为构造方法,特点有和类的名字相同,没有返回类型,也没有void。
public class Animal { String name; int age; //无参构造 public Animal(){ this.name = "旺财"; } //有参构造 public Animal(String name){ this.name = name; } public void run(){ System.out.println(name+"在跑"); } } -
封装
追求“高内聚,低耦合”,高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合就是暴露少量的方法给外部使用。
封装就是对数据隐藏
属性私有,get/set
public class Animal { String name; int age; public void run(){ System.out.println(name+"在跑"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } -
继承
只有单继承,没有多继承,就相当于人只有一个亲生爸爸。
子继承父用extends表示,子拥有父的所有方法
所有类都间接继承Object
super与this,super代表父类的东西,this指当前的东西
调用父类的构造器,必须在子类的构造器前面。
-
super调用父类的构造方法,必须在构造方法的第一个。
-
super必须只能出现在子类的方法或者构造方法中。
-
super和this不能同时调用构造方法
-
与this的区别:
代表的对象不同:
this:本身调用着这个对象
super:代表父类对象的应用。
前提:
this:没有继承也能用
super:只能在继承条件下才可以用
构造方法
this():本类的构造
super():父类的构造
-
-
方法重写
重写都是方法的重写,和属性无关
重写:需要有继承关系,子类重写父类的方法,子类的方法和父类必须一致,方法体不同
- 方法名相同
- 参数列表相同
- 修饰符范围:public>protected>default>private
- 抛出异常:范围可以缩小,不能扩大,ClassNotFoundException------>Exception(大)
为什么重写?父类的功能,子类不一定需要
父类的引用指向了子类
@Override//重写,注解:有功能的注释
静态方法与非静态方法的区别
- 静态方法:方法的调用只和右边,定义的数据类型有关
- 非静态:重写
-
多态
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系
- 存在条件:继承关系,方法需要重写,父类引用指向子类对象、
- instanceof(类型转换)引用类型

浙公网安备 33010602011771号