1 方法(三)
案例:Demo1
import java.util.Scanner; public class Demo1 { static int min(int num1,int num2){ if(num1<=num2){ return num1; }else{//num1>num2 return num2; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第1个整数:"); int num1 = sc.nextInt(); System.out.println("请输入第2个整数:"); int num2 = sc.nextInt(); int result = min(num1,num2); System.out.println("result="+result); } }
编写一个带两个int类型参数一个int类型返回值的方法min(),int min(int num1,int num2),在min方法中求出其中较小的整数并返回到调用的位置。在main方法中,创建Scanner对象,分别输入两个整数作为实参,调用min()方法,并输出返回的结果。
综合案例:Demo2
从控制台输入任意的三个整数(正数),分别作为三角形的三条边,根据边长,确定是等边三角形,等腰三角形,直角三角形,普通三角形
编写一个带三个参数没有返回值的方法f1(int a,int b,int c),
把a,b,c作为三角形三条边,输出对应等边三角形,等腰三角形,直角三角形,普通三角形
在main方法中,从控制台输入三个整数(正数)作为实参调用f1()方法
if(a+b>c && b+c>a && a+c>b){
if(a==b||b==c||a==c){
if(a==b&&b==c&&a==c){
等边三角形
}else{
等腰三角形
}
}else if(a*a+b*b==c*c||...){
直角三角形
}else{
普通三角形
}
}else{
不能构成三角形
}
import java.util.Scanner; public class Demo2 { static void f1(int a,int b,int c){ if(a+b>c && b+c>a && a+c>b){ if(a==b || b==c || a==c){ if(a==b && b==c && a==c){ System.out.println("等边三角形"); }else{ System.out.println("等腰三角形"); } }else if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a){ System.out.println("直角三角形"); }else{ System.out.println("普通三角形"); } }else{ System.out.println("不能构成三角形"); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入第1条边长:"); int a = sc.nextInt(); System.out.println("请输入第2条边长:"); int b = sc.nextInt(); System.out.println("请输入第3条边长:"); int c = sc.nextInt(); f1(a,b,c); } }
2 类和对象
1)类:指代一类事物,是一个抽象的概念,比如人类,车类,植物类
2)对象:指代一个真实存在的具体的事物,比如刘诗诗,门口那辆奔驰,桌子上的仙人掌
3)类和对象之间的关系:类是对象的模板,根据类来创建一个个的对象,对象是类的一个具体实现,具备类中所有特征和行为(功能)。
3 类的组成
特征和行为(功能)
eg:
人类:
特征:姓名,年龄,性别,身高,体重...
行为:睡觉,吃饭,踢足球...
微信:
特征:微信名,二维码,密码,红包,好友,朋友圈...
行为:聊天,添加好友,发红包,抢红包,发朋友圈...
4 Java中的类
1)类的组成:属性和方法
属性:特征,变量
方法:去掉static
2)格式:
class 类名{
属性:变量
数据类型 属性名;
方法:
返回值类型 方法名(参数列表){
方法体
}
}
class Person{
//属性:姓名,性别,年龄
String name;
String sex;
int age;
//方法: show():输出每一个属性
void show(){
System.out.println(name+","+sex+","+age);
}
}
3) 类就是把属性和对属性操作的方法封装在一起。方法可以访问自己的属性。
案例:Demo3
public class Demo3 { public static void main(String[] args) { //类名 对象名 = new 类名() 暂时 Person p1 = new Person(); //对象名.属性名 p1.name = "刘备"; p1.age = 29; p1.job = "皇叔"; //对象名.方法名(参数) p1.show(); Person p2 = new Person(); p2.name = "曹操"; p2.age = 32; p2.job = "丞相"; p2.show(); } } class Person{ //属性:姓名,年龄,职位(job) String name; int age; String job; //方法 void show(){ System.out.println("姓名:"+name); System.out.println("年龄:"+age); System.out.println("职位:"+job); } } //1 类和对象 //1)类:指代一类事物,是一个抽象的概念,比如人类... //2)对象:指代一个具体的真实存在的事物,比如谦哥... //3)类和对象的关系:类是对象的模板,根据类创建一个个 //的对象;对象是类的一个具体实现,对象会具备类中所有 //属性和方法。 //2 类的组成:特征和行为(功能) //3 Java中的类: //1)组成:属性和方法 //属性:特征,本质就是变量,在方法外定义的。 //方法:去掉static //2)格式: //class 类名{ //属性: //数据类型 属性名; //方法: //返回值类型 方法名(参数列表){ // 方法体 //} //} //3)Java中的类就是把属性和对属性操作的方法封装在 //一起,自己的方法可以访问自己的属性。
5 Java中的对象
当程序运行的时候,对象就会真实的存在在于内存中,而且对象会具备类中所有的属性和方法。
1)创建对象(暂时):
类名 对象名 = new 类名();
eg:
Person p1 = new Person();
2)访问对象的属性和方法:
a)访问对象的属性:
对象名.属性名
eg:
p1.name = "李世民";
b)访问对象的方法:
对象名.方法名(参数)
eg:
p1.show();
案例:Demo4
public class Demo4 { public static void main(String[] args) { User user1 = new User(); user1.name = "诸葛亮"; user1.age = 31; user1.password = "123456"; user1.job = "军师"; user1.show(); User user2 = new User(); user2.name = "郭嘉"; user2.age = 21; user2.password = "abc"; user2.job = "军师"; user2.show(); } } class User{ //属性 String name; int age; String password; String job; //方法 void show(){ System.out.println(name+","+age+"," +password+","+job); } } public class Demo5 { public static void main(String[] args) { Customer cus1 = new Customer(); cus1.name = "吕布"; cus1.age = 32; cus1.job = "将军"; cus1.address = "长安"; cus1.salary = 200000.1; cus1.show(); cus1.showAll(); Customer cus2 = new Customer(); cus2.name = "貂蝉"; cus2.age = 21; cus2.job = "美女"; cus2.address = "长安"; cus2.salary = 200000.1; cus2.show(); cus2.showAll(); } } class Customer{ //属性 String name; int age; String job; double salary; String address; //方法 void show(){ System.out.println(name+","+age+"," +job); } void showAll(){ System.out.println(name+","+age+"," +job+","+salary+","+address); } }
6 面向对象编程的一般方式
首先根据需求设计类,主要编写类中的属性和方法,然后创建该类的对象,并给属性一一赋值,最后调用其方法,实现功能。
案例:Demo6
public class Demo6 { public static void main(String[] args) { Box box1 = new Box(); box1.length = 100; box1.width = 80; box1.height = 90; box1.show(); box1.showTiji(); Box box2 = new Box(); box2.length = 200; box2.width = 120; box2.height = 150; box2.show(); box2.showTiji(); } } class Box{ //属性 int length; int width; int height; //方法 void show(){ System.out.println(length+","+width +","+height); } void showTiji(){ int result = length * width * height; System.out.println("体积是"+result); } } public class Demo7 { public static void main(String[] args) { Emp emp1 = new Emp(); emp1.name = "李治"; emp1.age = 32; emp1.job = "皇帝"; emp1.address = "洛阳"; emp1.salary = 20000000.1; emp1.email = "zhi@126.com"; emp1.show(); emp1.showYearSal(); Emp emp2 = new Emp(); emp2.name = "李隆基"; emp2.age = 18; emp2.job = "皇帝"; emp2.salary = 1000000.1; emp2.email = "long@163.com"; emp2.address = "长安"; emp2.show(); emp2.showYearSal(); } } class Emp{ //属性 String name; int age; String job; String address; String email; double salary; //方法 void show(){ System.out.println(name+","+age+","+ job+","+address+","+email+ ","+salary); } void showYearSal(){ double result = salary * 12; System.out.println(name+"的年薪是"+ result); } } public class Demo8 { public static void main(String[] args) { Kongtiao kt = new Kongtiao(); kt.name = "格力"; kt.color = "白色"; kt.price = 2000.1; kt.degree = 27; kt.type = "壁挂式"; kt.show(); kt.hot(); kt.hot(); kt.cool(); kt.cool(); kt.cool(); } } class Kongtiao{ //属性 String name; String type; double price; String color; int degree; //方法 void show(){ System.out.println(name+","+type+"," +price+","+color+","+degree); } void hot(){ degree++; System.out.println(name+"升高1度以后," +"当前的温度是"+degree); } void cool(){ degree--; System.out.println(name+"降低1度以后," +"当前的温度是"+degree); } }
7 堆和栈(了解)
在Java中使用堆和栈保存引用类型对象的存储区域。
1) 堆:真实保存对象的存储空间。
2) 栈:保存堆中对象的地址。
8 构造方法
构造方法是一种特殊方法,只有在创建对象的时候才会被调用,执行初始化操作,比如给属性赋值。
1)构造方法的名字跟类名一致,而且构造方法没有返回值,所以也就没有返回值类型。
2)格式:
类名(参数列表){
方法体
}
class Person2{
//属性
String name;
int age;
String job;
//构造方法
Person2(String name1,int age1,String job1){
name = name1;
age = age1;
job = job1;
}
}
main(){
Person2 p2 = new Person2("张三丰",100,"道士")
}
3) 创建对象的标准格式:
类名 对象名 = new 构造方法