假设我有一个班级:
class test {
public static function sayHi() {
echo 'hi';
}
}
通过test::sayHi();
我可以在班级之外定义sayHi()
,也可以完全摆脱班级?
public static function sayHi() {
echo 'hi';
}
我只需要这个函数来封装脚本的代码。 也许答案不是静态方法,而是普通函数def?
答案 0 :(得分:1)
没有类的静态方法根本没有任何意义。静态关键字表示此方法对于类的所有实例都是相同的。这使你能够在类本身而不是在其中一个实例上静态地调用它。
如果方法的内容是自包含的,意味着它不需要该类的任何其他静态变量或方法,则可以简单地省略该类并将代码放在全局方法中。使用全局方法被认为是一种不好的做法。
所以我的建议就是保持这个课程,即使它只有一个方法。这样您仍然可以自动加载文件,而不必自己需要。
答案 1 :(得分:0)
默认情况下,OOP中的函数是公共的,您可以将它们修改为私有,如:
m=12; n=12; % includes boundary nodes, mesh spacing 1/(m-1) and 1/(n-1)
[x,y]=ndgrid((0:m-1)/(m-1),(0:n-1)/(n-1)); % matlab forms x and y lists
p=[x(:),y(:)]; % N by 2 matrix listing x,y coordinates of all N=mn nodes
nelems=(m-1)*(n-1);
t=zeros(nelems,4);
a=0;
for e=1:nelems
t(e) = e + a;
t(e, 2) = e + a + 1;
t(e, 3) = e + a + m + 1;
t(e, 4) = e + a + m;
a = floor(e/n);
end
% final t lists 4 node numbers of all rectangles in T by 4 matrix
b=[1:m,m+1:m:m*n,2*m:m:m*n,m*n-m+2:m*n-1]; % bottom, left, right, top
% b = numbers of all 2m+2n **boundary nodes** preparing for U(b)=0
N=size(p,1);T=nelems; % number of nodes, number of rectangles
% p lists x,y coordinates of N nodes, t lists rectangles by 4 node numbers
K=sparse(N,N); % zero matrix in sparse format: zeros(N) would be "dense"
F=zeros(N,1); % load vector F to hold integrals of phi's times load f(x,y)
for e=1:T % integration over one rectangular element at a time
nodes=t(e,:); % row of t = node numbers of the 3 corners of triangle e
Pe=[ones(4,1),p(nodes,:),p(nodes,1).*p(nodes,2)]; % 4 by 4 matrix with rows=[1 x y xy]
Area=abs(det(Pe(1:3,1:3))); % area of triangle e = half of parallelogram area
C=inv(Pe); % columns of C are coeffs in a+bx+cy to give phi=1,0,0 at nodes
% now compute 3 by 3 Ke and 3 by 1 Fe for element e
% grad=C(2:3,:);
% constantKe=Area*grad'*grad; % element matrix from slopes b,c in grad
for i=1:4
for j=1:4
syms x y
Kn = int(int( ...
C(i,2)*C(j,2)+ ...
(C(i,2)*C(j,4)+C(i,4)*C(j,2))*y + ...
C(i,4)*C(j,4)*y^2 + ...
C(i,3)*C(j,3) + ...
(C(i,4)*C(j,3)+C(i,3)*C(j,4))*x + ...
C(i,4)*C(j,4)*x^2 ...
, x, Pe(1, 2), Pe(2, 2)), y, Pe(1, 3), Pe(3, 3));
K(nodes(i),nodes(j)) = K(nodes(i),nodes(j)) + Kn;
end
end
Fe=Area / 3; % integral of phi over triangle is volume of pyramid: f(x,y)=1
% multiply Fe by f at centroid for load f(x,y): one-point quadrature!
% centroid would be mean(p(nodes,:)) = average of 3 node coordinates
% K(nodes,nodes)=K(nodes,nodes)+Ke; % add Ke to 9 entries of global K
F(nodes)=F(nodes)+Fe; % add Fe to 4 components of load vector F
end % all T element matrices and vectors now assembled into K and F
% [Kb,Fb] = dirichlet(K,F,b) % assembled K was singular! K*ones(N,1)=0
% Implement Dirichlet boundary conditions U(b)=0 at nodes in list b
K(b,:)=0; K(:,b)=0; F(b)=0; % put zeros in boundary rows/columns of K and F
K(b,b)=speye(length(b),length(b)); % put I into boundary submatrix of K
Kb=K; Fb=F; % Stiffness matrix Kb (sparse format) and load vector Fb
% Solving for the vector U will produce U(b)=0 at boundary nodes
U=Kb\Fb; % The FEM approximation is U_1 phi_1 + ... + U_N phi_N
U2=reshape(U',m,n);
% Plot the FEM approximation U(x,y) with values U_1 to U_N at the nodes
surf(U2)
或者就像你说的那样,静态,公共或私人,如:
private function test(){
//do something
}
但是,如果您不使用OOP,默认情况下功能是全局和公开的,您无法更改其对私有的访问权限。这不是它们的工作方式,因为如果你将它们的类型改为私有,你将永远无法访问该功能。另外,静态不起作用,因为是OOP的另一个属性......
然后,您可以简单地创建该功能并从您想要的任何地方访问它(显然可用的位置:P,因为您需要包含存储的文件)