在java中的另一个方法中从构造函数调用变量

时间:2013-11-26 10:23:28

标签: java methods constructor

如果我有一个包含变量的构造函数。如何在同一个类中的另一个方法中调用该变量?

在下面的示例中,如果col大于构造函数中的Columnnum,我希望代码在checkBoundaries方法中返回false。

    import java.util.Arrays;
    class MovieSeating 
    {
        private String[][] Seats;

        public MovieSeating(int rowNum, int columnNum)
        {
            Seats = new String[rowNum][columnNum];
            for (int r = 0; r < rowNum; r++)
            {
                for (int c = 0; c < columnNum; c++)
                {
                    Seats[r][c] = "?.?";
                    System.out.print(Seats[r][c] + " ");
                }
                System.out.println("\n");
            }

            System.out.println("Seating is empty.");



        }

        public boolean checkBoundaries(int row, int col)
        {
            if (col < 0 || row < 0 || col >= columnNumInConstructorParameter)
            {
                return false;
            }
            else {
                return true;
            }
        }

6 个答案:

答案 0 :(得分:2)

您可以使用:

if (col < 0 || row < 0 || col >= Seats[0].length)

通常,如果要在方法中使用变量,则必须将其设置为实例变量。

答案 1 :(得分:1)

将您的变量变为私有实例成员并调用它们。

离。

public class Foo {

    private int myVariable = 0;

    public Foo(int myVariable) {
        this.myVariable = myVariable;
    }

    public boolean positiveValue() {
        return this.myVariable > 0;
    }

}

答案 2 :(得分:1)

class MovieSeating 
{
    private String[][] Seats;
    private int rowNumber = 0;
    private int columnNumber = 0;

    public MovieSeating(int rowNum, int columnNum)
    {
        this.rowNumber = rowNum;
        this.columnNumber = columnNum;
        Seats = new String[rowNum][columnNum];
        for (int r = 0; r < rowNum; r++)
        {
            for (int c = 0; c < columnNum; c++)
            {
                Seats[r][c] = "?.?";
                System.out.print(Seats[r][c] + " ");
            }
            System.out.println("\n");
        }

        System.out.println("Seating is empty.");



    }

    public boolean checkBoundaries(int row, int col)
    {
        if (col < 0 || row < 0 || row >= rowNumber || col >= columnNumber)
        {
            return false;
        }
        else {
            return true;
        }
    }
}

答案 3 :(得分:0)

只需通过参数传递

public Classname(){
    String foo = "bar";        
    myMethod(foo);
}
public void myMethod(String parameter){
    //do something
}

答案 4 :(得分:0)

将构造函数参数保存到实例变量中总是更好的做法。

答案 5 :(得分:0)

将int rowNum,int columnNum存储为实例变量可能很有用。但是,可以从String [] [] Seats变量

的大小访问它们

考虑添加以下内容:

foreach (var item in listOfImgdata )
            {
                var imageData = Convert.FromBase64String(item);
                var contentId = Guid.NewGuid().ToString();
                LinkedResource inline = new LinkedResource(new MemoryStream(imageData), "image/jpeg");
                inline.ContentId = contentId;
                inline.TransferEncoding = TransferEncoding.Base64;
                //Replace all img tags with the new img tag 
                htmlBody = Regex.Replace(htmlBody, "<img.+?src=[\"'](.+?)[\"'].*?>", @"<img src='cid:" + inline.ContentId + @"'/>");
            }