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);
}
}
}
数组:就是一个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的核心思想就是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():父类的构造
方法重写
重写都是方法的重写,和属性无关
重写:需要有继承关系,子类重写父类的方法,子类的方法和父类必须一致,方法体不同
为什么重写?父类的功能,子类不一定需要
父类的引用指向了子类
@Override//重写,注解:有功能的注释
静态方法与非静态方法的区别
多态
原文:https://www.cnblogs.com/xiao-wei-ge/p/12399565.html