上课笔记
public class thisDemo {
int a,b,c;
public thisDemo(){
System.out.println("正要new一个Hello对象");
}
public thisDemo(int a,int b){
this();//调用无参的构造方法,并且必须位于第一行
this.a =a;
this.b =b;
}
public thisDemo(int a,int b,int c){
this(a,b);//调用无参的构造方法,并且必须位于第一行
this.c =c;
}
public void sing(){
System.out.println("这是一个无参的构造方法");
}
public void eat(){
this.sing();//调用无参的构造方法,必须位于第一行
System.out.println("你妈妈喊你回家吃饭");
}
public static void main(String[] args) {
thisDemo t1 =new thisDemo();
t1.eat();
}
}
public class staticDemo {
int a;
static int width;
public static void tt(){
staticDemo t2 =new staticDemo();
t2.dd();//静态方法中不能直接调用非静态方法,需要创建一个对象然后对象.方法名//
System.out.println("这是一个静态的方法(this is a static method)");
}
public void dd(){
//staticDemo.tt();//非静态方法里可以直接调用静态方法,类名.方法名
System.out.println("这是一个非静态的方法(this is a non-static method)");
}
public static void main(String[] args) {
staticDemo t1= new staticDemo();
t1.width =2;
staticDemo.width =2;
/*staticDemo.tt();//调用静态方法的时候,可以使用类名.方法名,也可以使用对象名.方法名,建议使用前者*/
staticDemo.tt();//调用非静态的方法的时候只能创建一个对象,然后对象.方法名//
tt();
t1.dd();
}
}
public class Static {
static int count;
// int count;
public Static(){
count++;
System.out.println("创建了" +count + "个对象");
}
public static void main(String[] args) {
new Static();//1
new Static();//2
new Static();//3
}
}
public void show(){
System.out.println("姓名:"+this.name+"\t年龄:"+this.age+"\t城市:"+this.country);
}
public static void welcome(){
this.show();//调用本类的非静态方法
System.out.println("欢迎大家学习java......");
}
public class Dog {
private String name = "哈哈"; // 昵称
private int health = 100; // 健康值
private int love = 0;
public void play(int n) {
static int localv=5;//在方法里不可以定义static变量
health = health - n;
System.out.println(name+" "+localv+" "+health+" "+love);
}
public static void main(String[] args) {
Dog c=new Dog();
c.play(5);
}
}
public class Static1 {
String name = "zhaoyun";
static int age = 20;
// public Static1(String name,int age){
// this.name = name;
// this.age = age;
// }
public void test(){
System.out.println("test1 is a non-static method");
// static int a = 20;
// test2();
}
public static void test1(){
// this.test1();
System.out.println("test2 is a static method");
// static int a = 10;
// test1();
new Static1().test(); //间接调用
}
public static void main(String[] args) {
Static1 sd = new Static1();
System.out.println(sd.name);
System.out.println(sd.age);
// sd.age = 35;
// System.out.println(sd.age);//35
// System.out.println(sd.age);//35
//
// sd.age = 50;
// System.out.println(sd.age);//50
// System.out.println(Static1.age);//50
Static1 staticDemo = new Static1();
staticDemo.test();
// StaticDemo.test1();
// staticDemo.test1();
// Static1 sd2 =new Static1();
// System.out.println(sd2.name);
}
}
public class StaticBlock {
static {
System.out.println("此处,可执行类的初始化工作!");
}
public static void main(String[] args) {
System.out.println("main方法中的第一句");
}
}
public class CodeBlock {
int x;
int y;
{
System.out.println("构造代码块");
}
static{
System.out.println("static代码块");
// x = 0;
}
public CodeBlock(){
}
public CodeBlock(int x){
System.out.println("CodeBloc(int x)被执行了");
this.x = x;
}
public CodeBlock(int x,int y){
this(x);
System.out.println("CodeBlock(int x,int y)被执行了");
// this.x = x;
this.y = y;
}
public void test(){
System.out.println("test方法被执行");
{
System.out.println("test方法中的普通代码块");
}
}
public synchronized void test(){
//多线程代码块
// synchronized(this){
//
// }
}
public static void main(String[] args) {
CodeBlock cbd = new CodeBlock();
cbd.test();
{
System.out.println("main方法中的普通代码块");
}
CodeBlock cbd2 = new CodeBlockDemo(1,2);
}
}
import static java.lang.Math.*
public class Import {
public void t(){
}
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// java.util.Arrays.sort(new int[]{1,3,5,4,2});
//使用某包中的Person类
// Person person = new Person();
// Date date = new Date();
// long time = 12332423;
// java.sql.Date date1 = new java.sql.Date(time);
// System.out.println(Math.random());
// System.out.println(Math.sqrt(6));
// System.out.println(Math.abs(-3));
//静态导包可以省略类名
System.out.println(random());
System.out.println(sqrt(2));
System.out.println(Math.abs(-1));
}
}
/*private String name;
private String strain;
private int health;
public Dog(String name,String strain,int health){
this.name =name;
this.strain =strain;
this.health =health;
}
public Dog(){}
public void setName(String name){
this.name =name;
}
public String getName(){
return name;
}
public void setStrain(String strain) {
if ("1".equals(strain)) {
this.strain = "拉布拉多犬";
}else if("2".equals(strain)) {
this.strain = "高加索犬";
}else {
System.out.println("清选择1或2,默认的是高加索犬");
this.strain = "高加索犬";
}
}
public String getStrain() {
return strain;
}
public void setHealth(int health){
if (health >100||health<1){
this.health =50;
System.out.println("狗狗的健康值应该在1到100之间,默认值是50");
}else{
this.health =health;
}
}
public int gethealth(){
return health;
}
static Dog d1 =new Dog();
public void show(){
System.out.println("昵称:"+getName());
System.out.println("品种:"+getStrain());
System.out.println("健康值:"+gethealth());
}
Dog(String name,String strain){}
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.println("请输入狗狗的昵称:");
String name =input.next();
System.out.println("请输入狗狗品种对应的数字:");
String strain =input.next();
System.out.println("请输入狗狗的健康值:");
int health =input.nextInt();
System.out.println("狗狗信息如下:");
d1.setName(name);
d1.setStrain(strain);
d1.setHealth(health);
d1.show();
}*/
上机练习作业(All)
//上机练习1
public class Dog {
private String type;
private String name;
private int health;
private int love;
private String strain;
public void print() {
System.out.println("类型:"+getType()+ "\t\t昵称" + getName()+ "\t健康值:"+getHealth()+ "\t亲密度:"+getLove()+ "\t品种:"+getStrain() );
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLove(int love) {
this.love = love;
}
public int getLove() {
return love;
}
public void setStrain(String strain) {
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setType(String type){
this.type =type;
}
public String getType(){return type;}
}
public class Penguin {
private String type;
private String name;
private int health;
private int love;
private String strain;
public void print() {
System.out.println("类型:"+getType()+"\t昵称" + getName()+"\t健康值:"+getHealth()+"\t亲密度:"+getLove()+"\t品种:"+getStrain() );
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLove(int love) {
this.love = love;
}
public int getLove() {
return love;
}
public void setStrain(String strain) {
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setType(String type){
this.type =type;
}
public String getType(){return type;}
}
public class test {
public static void main(String[] args) {
Dog t1 =new Dog();
t1.setType("狗");
t1.setName("卡卡");
t1.setHealth(100);
t1.setLove(80);
t1.setStrain("哈士奇");
t1.print();
Penguin t2 =new Penguin();
t2.setType("企鹅");
t2.setName("库奇");
t2.setHealth(80);
t2.setLove(40);
t2.setStrain("北极");
t2.print();
}
}
//上机练习2
public class dog{
/*private String name;
private String strain;
private int health;
public Dog(String name,String strain,int health){
this.name =name;
this.strain =strain;
this.health =health;
}
public Dog(){}
public void setName(String name){
this.name =name;
}
public String getName(){
return name;
}
public void setStrain(String strain) {
if ("1".equals(strain)) {
this.strain = "拉布拉多犬";
}else if("2".equals(strain)) {
this.strain = "高加索犬";
}else {
System.out.println("清选择1或2,默认的是高加索犬");
this.strain = "高加索犬";
}
}
public String getStrain() {
return strain;
}
public void setHealth(int health){
if (health >100||health<1){
this.health =50;
System.out.println("狗狗的健康值应该在1到100之间,默认值是50");
}else{
this.health =health;
}
}
public int gethealth(){
return health;
}
static Dog d1 =new Dog();
public void show(){
System.out.println("昵称:"+getName());
System.out.println("品种:"+getStrain());
System.out.println("健康值:"+gethealth());
}
Dog(String name,String strain){}
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.println("请输入狗狗的昵称:");
String name =input.next();
System.out.println("请输入狗狗品种对应的数字:");
String strain =input.next();
System.out.println("请输入狗狗的健康值:");
int health =input.nextInt();
System.out.println("狗狗信息如下:");
d1.setName(name);
d1.setStrain(strain);
d1.setHealth(health);
d1.show();
}*/
public class Dog1 {
private String name="憨憨";
private int health =100;
private int love =0;
private String strain ="哈士奇";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public void setHealth(int health){
if (health >100 || health<0){
this.health =40;
System.out.println("健康值应该在0-100之间,默认值是40");
}
else{
this.health =health;
}
}
public int getHealth(){
return health;
}
public void show(){
System.out.println("姓名:"+getName());
System.out.println("品种:"+getStrain());
System.out.println("健康值:"+getHealth());
}
public static void main(String[] args) {
Dog d =new Dog();
d.setName("哈皮");
d.setStrain("哈士奇");
d.setHealth(100);
d.show();
}
//上机练习3(调用上面Dog1类的信息)
String name;
String strain;
public void Dog(String name,String strain){
this.name =name;
this.strain =strain;
}
public static void main(String[] args) {
Dog t1 =new Dog();
t1.show();
}
}
上机练习4
public class Penguin {
private String name;
private int love;
private int health;
static final String Man ="Q仔";
static final String Women ="Q妹";
static String sex;
public void setLove(int love){
this.love =love;
}
public int getLove(){
return love;
}
public void setHealth (int health){
this.health =health;
}
public int getHealth(){return health;}
public void setSex(String sex){
if (sex =="雄"){
this.sex=sex;
}else if (sex =="雌"){
this.sex =sex;
}else{
System.out.println("您输入的性别不合法,请重新输入!");
}
}
static Penguin t2 =new Penguin();
public static void setSex() {
t2.setSex();
}
public static String getSex(){
return sex;
}
public String getName(){
if (sex =="雄"){
this.name =Man;
}else if (sex =="雌"){
this.name =Women;
}else{
System.out.println("由于您输入的性别不合法无法识别姓名,请重新输入!");
}
return name;
}
public void show(){
System.out.println("姓名:"+getName());
System.out.println("性别:"+getSex());
System.out.println("亲密度:"+getLove());
System.out.println("健康值:"+getHealth());
}
}
public class testPengiun {
public static void main(String[] args) {
Penguin t2 =new Penguin();
t2.setLove(65);
t2.setHealth(80);
t2.setSex("雄");
t2.show();
}
}