是這樣的,Java這門課沒有給線上實驗評測平臺(我還專門上平臺上看了看),第一周作業出得挺好,也就算了,第二周作業形式差別有點大,全是基本概念題,確實有些東西聽課看書不怎么注意,既然給的是這種形式,我盡量鍛煉自己不開編譯器、人腦編譯的能力,
一個挺好的地方:浙大的PTA測驗平臺
0322補充,語法確實有一些還不熟練的地方..后面的編程題挺好..
00 運行結果題
第1道
import java.io.*;
public class abc {
public static void main(String args [ ])
{
AB s = new AB("Hello!","I love JA V A.");
System.out.println(s.toString());
}
}
class AB {
String s1;
String s2;
public AB(String str1, String str2)
{
s1 = str1; s2 = str2;
}
public String toString( )
{
return s1+s2;
}
}
預期輸出結果應該為:
Hello!I love JA V A.
考察的是java類、字串的拼接、toString()函式,
第2道
import java.io.* ;
public class abc {
public static void main(String args[ ])
{
int i, s = 0 ;
int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
for ( i = 0 ; i < a.length ; i ++ )
if ( a[i]%3 == 0 ) s += a[i] ;
System.out.println("s="+s);
}
}
預期結果:
s=180
考察回圈與拼接符,
第3道
import java.util.Scanner;
public class abc {
public static void main(String args[ ])
{
Scanner in =new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println("a="+a+"\nb="+b);
}
}
這道題有問題,應該輸入a和b的,
預期結果:
//輸入
4 5
//輸出
a=4
b=5
考察拼接符吧,
第4道
public class Test {
public static void main(String[] args)
{
int percent = 10;
tripleValue(percent);
System.out.println(percent);
}
public static void tripleValue(int x)
{
x = 3 * x;
}
}
預期結果;
10
考察,java函式的記憶體情況,參考傳參和形式傳參,這道題函式呼叫了,但是沒有回傳值,
第5道
假設類 A 有建構式 A(int a),在類 A 的其他建構式中呼叫該建構式用陳述句為 ( this(x) )
原因解釋:書上有例子:
public Employee(String name, int age, double salary){
this.name = name;
this.age = age;
this.salary = salary;
}
public employee(){
this("劉明",25,3000);
}
//如果是其他建構式呼叫建構式,這個this陳述句必須是函式里的第一行;
第6道?
public class Test {
public static void main(String[] args)
{
new Test();
}
static int num = 4;
{
num += 3;
System.out.println("b");
}
int a = 5;
{ // 成員變數第三個
System.out.println("c");
}
Test() {
System.out.println("d");
}
static {
System.out.println("a");
}
static void run() {
System.out.println("e");
}
}
預期結果:
a
b
c
d
0322,這個沒怎么看明白,
0323,看了看網上有關static執行次序的講解1 / 講解2 / 講解3,類中執行順序的優先級為:靜態代碼塊>main>普通代碼塊>構造方法,靜態代碼塊在檔案被執行時,首先類就被初始化,這時候靜態代碼塊就被執行,輸出a b,newTest沒有輸出值,所以繼續普通代碼塊和構造方法,
虛擬機對Java類進行首次加載時,會對靜態代碼塊,靜態成員變數,靜態方法進行一次初始化加載,
-
三者的執行順序是:靜態成員變數,靜態代碼塊,最后執行靜態方法,為什么呢?方法肯定是被呼叫才執行,這個好說,變數和代碼塊呢?舉例,假如代碼塊中用到了變數怎么辦呢?所以肯定先加載成員變數, - 呼叫new方法才會創建類的實體
- 類實體創建程序中,按照父子關系進行初始化,首先是初始化父類靜態代碼塊,然后子類靜態代碼塊,然后依次是
-
父類的初始化代碼塊,父類構造方法,子類初始化代碼塊,子類構造方法 - 類實體銷毀的時候也是先銷毀子類,然后是父類
- 所以我大膽推測,如果把
new Test()去掉,那么就只有a輸出 - 如果main函式里改為只有
Test.run(),那么只有a e 輸出 - 扔進編譯器結果都對,<(^-^)>!
第 7 題
public class x {
private static int a;
public static void main(String[] args)
{
modify(a);
System.out.println(a);
}
public static void modify(int a)
{
a++;
}
}
預期結果:
0
0322:這個似懂非懂,
0323:從第 6 題,第 7 題頁很好理解,modify函式被呼叫了,但是沒有輸出a,a此時只是形參,函式結束后a++的臨時結果隨著函式銷毀而銷毀了,沒有傳回main函式,所以main函式里的a還是0,
01 程式設計題
第一題
題目
請寫一個程式, 類名叫 HelloWorld,類里面有一個成員方法 sayHello(),這個方法能向控制臺輸出 HelloWorld,
代碼
package homework_week2;
/**
*
* @author zzrs123
*/
public class Homework_week2 {
public static void main(String args[]){
Helloword test = new Helloword();
// System.out.println(test);
}
private static class Helloword {
public Helloword() {
SayHello();
}
void SayHello(){
System.out.println("Helloworld");
}
}
輸出結果正常,但感覺很怪,倒不是因為輸出不在main函式,而是因為剛構造完物件,就結束了...
第二題
這道題有點意思,
題目
宣告并測驗一個復數類,其方法包括 toString() 以及復數的加減乘除運算,
代碼
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package homework_week2_1;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.String.valueOf;
/**
*
* @author zzrs123
*/
public class Complex {
/**
* @param args the command line arguments
*/
public double real;
public double imagine;
//建構式,后來發現必須需要一個無參建構式
public Complex(){
this.real = 0;
this.imagine = 0;
}
public Complex(double real,double imagine){
this.real = real;
this.imagine = imagine;
}
public Complex addtwocomplex(Complex x, Complex y){
Complex sum = new Complex(x.real+y.real,x.imagine+y.imagine);
return sum;
}
public Complex subtwocomplex(Complex x,Complex y){
Complex sub = new Complex(x.real-y.real,x.imagine-y.imagine);
return sub;
}
public Complex muxtwocomplex(Complex x,Complex y){
Complex mux = new Complex(x.real*y.real-x.imagine*y.imagine, x.real*y.imagine+x.imagine*y.real);
return mux;
}
//突發奇想想算出來模平方和共軛,再來算除法
// public double squcomplex(Complex x){
// return Math.pow(x.real,2)+Math.pow(x.imagine, 2);
// }
// public Complex adjcomplex(Complex x){
// return new Complex(x.real,-x.imagine);
// }
//然后發現更麻煩了,下面的報錯是因為復數類和double不能相除,要實作還得另外寫函式定義這種運算
// public Complex exctwocomplex(Complex x,Complex y){
// Complex exc = muxtwocomplex(x,adjcomplex(y))/squcomplex(y);
public Complex divtwocomplex(Complex x,Complex y){
//除數不能為0
if(y.real==0&&y.imagine==0){
System.out.println("除數為0,錯誤");
System.exit(0);
}
else{
double real1 = x.real*y.real-x.imagine*y.imagine;
double imagine1 = x.real*y.imagine+x.imagine*y.real;
double squre = Math.pow(y.real, 2)+Math.pow(y.imagine, 2);
return new Complex(real1/squre,imagine1/squre);
}
return null;
}
public String toString(){//一定記得這里是無參的,否則復寫失敗
//第一次寫的時候沒有考慮負數以及兩個都為0的情況
//想讓輸出的標準一點情況還挺多的
if(this.real==0.0 && this.imagine==0.0){
return valueOf(0.0);
}
else if(this.real==0.0){
return this.imagine+"i";
}
else if(this.imagine==0.0){
return valueOf(this.real);
}
else if(this.imagine < 0){
return this.real+""+this.imagine+"i";
}
else{
return this.real+"+"+this.imagine+"i";
}
//return this.real+"+"+this.imagine+"i";
}
public static void main(String args[]) throws IOException{
double a1, b1;
double a2, b2;
Scanner in = new Scanner(System.in);
a1 = in.nextDouble();
b1 = in.nextDouble();
a2 = in.nextDouble();
b2 = in.nextDouble();
Complex com1 = new Complex(a1,b1);
Complex com2 = new Complex(a2,b2);
System.out.println("你輸入的兩個復數是:");
System.out.println(com1.toString());
System.out.println(com2.toString());
System.out.println("兩復數之和為:");
Complex add = new Complex();
add = com1.addtwocomplex(com1,com2);
System.out.println(add.toString());
System.out.println("兩復數之差為: ");
Complex sub = new Complex();
sub = com1.subtwocomplex(com1, com2);
System.out.println(sub.toString());
System.out.println("兩復數之積為: ");
Complex mux = new Complex();
mux = com1.muxtwocomplex(com1, com2);
System.out.println(mux.toString());
System.out.println("兩復數之商為: ");
Complex div = new Complex();
div = com1.divtwocomplex(com1, com2);
System.out.println(div.toString());
}
}
02 問答題
題目
“static”關鍵字是什么意思?java 中是否可以覆寫(override)一個 private 或者是 static 的方法?
解答
“static”關鍵字修飾的成員變數或者成員方法可以直接通過類名訪問,
方法多載Overloading和方法覆寫Overriding的區別:
- Overloading:兩個方法的方法名相同,引數表不一樣;
- Overriding:在子類中定義一個方法,正好跟父類中的一個方法一模一樣(回傳型別、引數表、名字),那么子類優先使用子類中的這個方法,這時候就發生了覆寫Overriding,
解答在教材《Java語言程式設計》的P129頁,
父類中的static可以被繼承,但不能被覆寫,如果子類定義了跟父類一模一樣的方法,那么父類的static方法被隱藏,
private方法不可以覆寫,因為private修飾的變數和方法只能在當前類中使用,不能被繼承和被其他的類訪問,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448066.html
標籤:Java
下一篇:Spring 學習筆記
