目录
Java?SE文章参考:Java SE入门及基础知识合集-CSDN博客
public class Computer {????????public String brand ; // 品牌????????public String type ; // 型号????????public double price ; // 价格}
public class ComputerTest {????????public static void main ( String [] args ){????????????????Computer c1 = new Computer ();????????????????c1 . brand = " 联想 " ;????????????????c1 . type = "T430" ;????????????????c1 . price = 5000 ;????????????????Computer c2 = new Computer ();????????????????c2 . brand = " 联想 " ;????????????????c2 . type = "W530" ;????????????????c2 . price = 6000 ;????????????????Computer c3 = new Computer ();????????????????c3 . brand = " 联想 " ;????????????????c3 . type = "T450" ;????????????????c3 . price = 7000 ;????????}}
访问修饰符 类名 ( 数据类型 1 变量名 1 , 数据类型 2 变量名 2 ,... 数据类型 n 变量名 n ){}
/*** 计算机*/public class Computer {????????public String brand ; // 品牌????????public String type ; // 型号????????public double price ; // 价格????????//如果一个类中没有定义任何构造方法,那么编译器会自动为这个类添加一个默认的无参构造方法????????//如果一个类中定义了构造方法,那么编译器不会为这个类添加默认的无参构造方法????????//如果在一个类中已经定义了带参数的构造方法,此时还想使用无参构造方法,那么必须将无参构造方法也定义出来????????public Computer (){}????????//此时在类中定义了带参数的构造方法,那么编译器不会为这个类添加默认的无参构造方法????????//构造方法的 ()表示的是参数列表,这里的列表是形式参数????????????????public Computer ( String brand , String type , double price ){????????this . brand = brand ;????????this . type = type ;????????this . price = price ;????????}}public class ComputerTest {????????public static void main ( String [] args ) {????????Computer c1 = new Computer ();????????c1 . brand = " 联想 " ;????????c1 . type = "T430" ;????????c1 . price = 5000 ;????????//调用带参构造方法创建对象时,必须注意参数列表传递的值要与构造方法定义时的形式列表一一对应????????//传递的参数是实参:也就是形式参数的一个具体实例。????????Computer c4 = new Computer ( " 联想 " , "T430" , 5000 );????????Computer c2 = new Computer ();????????c2 . brand = " 联想 " ;????????c2 . type = "W530" ;????????c2 . price = 6000 ;????????Computer c5 = new Computer ( " 联想 " , "W530" , 6000 );????????????? Computer c3 = new Computer ();????????c3 . brand = " 联想 " ;????????c3 . type = "T450" ;????????c3 . price = 7000 ;????????Computer c6 = new Computer ( " 联想 " , "T450" , 7000 );????????}}
访问修饰符 返回值类型 方法名 ( 数据类型 1 变量名 1 , 数据类型 2 变量名 2 ,... 数据类型 n 变量名 n ){????????[ return 返回值 ;]}// 带参方法调用对象名 . 方法名 ( 实参 1 , 实参 2 ,... 实参 n );
public class Calculator {????????public int number1 ;????????public int number2 ;????????public String operator ;/*** 访问修饰符 返回值类型 方法名 ( 数据类型 1 变量名 1, 数据类型 2 变量名 2,... 数据类型 n变量名 n){* [return 返回值 ;]* }** return 关键字的作用就是给出方法执行的结果,使得方法直接结束*///calculate 方法执行完成后必须要返回一个 int 类型的值// 如果一个方法的返回值类型不为 void ,那么在选择结构中,必须为每一种情况都提供一个返回值????????public int calculate (){????????????????switch ( operator ){????????????????case "+" : return number1 + number2 ;????????????????case "-" : return number1 - number2 ;????????????????case "*" : return number1 * number2 ;????????????????case "/" : return number1 / number2 ;????????????????default : return 0 ;????????????????}????????}}
public class CalculatorTest {????????public static void main ( String [] args ){????????Calculator c = new Calculator ();????????c . number1 = 30 ;????????c . number2 = 72 ;????????c . operator = "*" ;????????????????int result1 = c . calculate ();????????c . number1 = result1 ;????????c . number2 = 3 ;????????c . operator = "/" ;????????int result2 = c . calculate ();????????System . out . println ( result2 );????????}}public class CalculatorTest {????????public static void main ( String [] args ) {????????????????Scanner sc = new Scanner ( System . in );????????????????System . out . println ( " 请输入你的姓名: " );????????????????String name = sc . next ();????????????????????????????????Calculator c = new Calculator (); // 构建一个计算器????????????????c . number1 = 30 ;????????????????c . number2 = 72 ;????????????????c . operator = "*" ;????????????????int total = c . calculate (); // 计算总价????????????????c . number1 = total ;????????????????c . number2 = 3 ;????????????????c . operator = "/" ;????????????????int avg = c . calculate ();????????????????System . out . println ( " 每天卖了 " + avg );????????????????}}
public class Calculator {????????public int number1 ;????????public int number2 ;????????public String operator ;????????public Calculator (){}????????public Calculator ( int number1 , int number2 , String operator ){????????????????this . number1 = number1 ;????????????????this . number2 = number2 ;????????????????this . operator = operator ;}/*** 访问修饰符 返回值类型 方法名 ( 数据类型 1 变量名 1, 数据类型 2 变量名 2,... 数据类型 n变量名 n){* [return 返回值 ;]* }** return 关键字的作用就是给出方法执行的结果,使得方法直接结束*///calculate 方法执行完成后必须要返回一个 int 类型的值// 如果一个方法的返回值类型不为 void ,那么在选择结构中,必须为每一种情况都提供一个返回值????????public int calculate (){????????????????switch ( operator ){????????????????case "+" : return number1 + number2 ;????????????????case "-" : return number1 - number2 ;????????????????case "*" : return number1 * number2 ;????????????????case "/" : return number1 / number2 ;????????????????default : return 0 ;????????????????}????????}}import java . util . Scanner ;/*** 某商家共有 30 件啤酒,每件价格 72 元,商家在 3 天内卖完这 30 件啤酒,请问每天卖了多少钱?*/public class CalculatorTest {????????public static void main ( String [] args ) {????????????????Scanner sc = new Scanner ( System . in );????????????????System . out . println ( " 请输入你的姓名: " );????????????????String name = sc . next ();????????????????Calculator c = new Calculator (); // 构建一个计算器????????????????c . number1 = 30 ;????????????????c . number2 = 72 ;????????????????c . operator = "*" ;????????????????????????????????int total = c . calculate (); // 计算总价????????????????c . number1 = total ;????????????????c . number2 = 3 ;????????????????c . operator = "/" ;????????????????int avg = c . calculate ();????????????????System . out . println ( " 每天卖了 " + avg );????????????????Calculator c1 = new Calculator ( 30 , 72 , "*" );????????????????int result = c1 . calculate ();????????????????Calculator c2 = new Calculator ( result , 3 , "/" );????????????????int avg1 = c2 . calculate ();????????????????System . out . println ( " 每天卖了 " + avg1 );????????}}